2020-01-26 00:50:18 +00:00
|
|
|
import socketIOClient from 'socket.io-client';
|
|
|
|
import config from './config';
|
|
|
|
|
2020-01-28 06:40:03 +00:00
|
|
|
const launch = (nsp, dispatch) => {
|
|
|
|
const socket = socketIOClient(`${config.socketAddress}/${nsp}`);
|
2020-01-28 22:33:54 +00:00
|
|
|
|
2020-01-26 00:50:18 +00:00
|
|
|
socket.on('connected', () => {
|
2020-01-28 06:40:03 +00:00
|
|
|
dispatch({ type:'SOCKET', message:'CONNECTED', body:{nsp: socket.nsp} });
|
2020-01-26 00:50:18 +00:00
|
|
|
});
|
|
|
|
socket.on('connect_error', err => {
|
2020-01-28 06:40:03 +00:00
|
|
|
dispatch({ type: 'ERR', message:'SOCKET_ERROR', body: { socketError: err }});
|
2020-01-26 00:50:18 +00:00
|
|
|
});
|
|
|
|
socket.on('error', err => {
|
2020-01-28 06:40:03 +00:00
|
|
|
dispatch({ type: 'ERR', message:'SOCKET_ERROR', body: { socketError: err } });
|
2020-01-26 00:50:18 +00:00
|
|
|
});
|
2020-01-28 22:33:54 +00:00
|
|
|
|
2020-01-26 00:50:18 +00:00
|
|
|
socket.on('room_connected', (data) => {
|
2020-01-28 22:33:54 +00:00
|
|
|
dispatch({ type: 'ROOMS', message: 'CONNECT_ROOM', body: data });
|
2020-01-26 00:50:18 +00:00
|
|
|
});
|
2020-01-28 22:33:54 +00:00
|
|
|
|
2020-01-26 00:50:18 +00:00
|
|
|
socket.on('new_user', (data) => {
|
2020-01-28 06:40:03 +00:00
|
|
|
console.log('new_user received')
|
2020-01-28 22:33:54 +00:00
|
|
|
dispatch({ type: 'ROOMS', message: 'NEW_USER', body: data })
|
2020-01-26 00:50:18 +00:00
|
|
|
})
|
|
|
|
|
2020-01-30 07:22:50 +00:00
|
|
|
socket.on('game_connected', (data) => {
|
|
|
|
console.log(data)
|
|
|
|
console.log('game_connected received')
|
2020-02-01 06:22:43 +00:00
|
|
|
dispatch({ type: 'GAMES', message: 'UPDATE_BOARD', body: data })
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('update_board', (data) => {
|
|
|
|
console.log(data)
|
|
|
|
console.log('update_board received')
|
|
|
|
dispatch({ type: 'GAMES', message: 'UPDATE_BOARD', body: data.board })
|
2020-01-30 07:22:50 +00:00
|
|
|
})
|
|
|
|
|
2020-01-26 00:50:18 +00:00
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
launch
|
|
|
|
}
|