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
|
|
|
})
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
launch
|
|
|
|
}
|