stub room socket connection (client + server)

This commit is contained in:
Sorrel Bri 2020-01-21 14:33:40 -08:00 committed by sorrelbri
parent ebb23863dd
commit 6c0d1fbc7c
4 changed files with 43 additions and 15 deletions

View file

@ -2,11 +2,18 @@ import React, { useEffect } from 'react';
import './Home.scss';
import roomsServices from '../../services/api/roomsServices';
import RoomButton from '../../components/Button/Room/Room';
import socketIOClient from 'socket.io-client';
import config from '../../config';
// ! [start] roomSocket stub
const roomId = 1
const roomSocket = socketIOClient(`${config.socketAddress}/${roomId}`)
// ! [end]
const Home = props => {
const state = props.state;
const state = props.state || {};
const dispatch = props.dispatch;
const renderRooms = () => {
const rooms = state.rooms || [];
if (rooms.length) {
@ -31,11 +38,24 @@ const Home = props => {
return dispatch(action)
}
}
useEffect(() => {
fetchRoomsAPI();
}, [])
// ! [start] roomSocket stub
const roomSocketConnect = () => {
roomSocket.emit('connect');
// roomSocket.on('connected', data => setSocketData('room socket connected'));
// roomSocket.on('connect_error', err => setError([...error, err]));
// roomSocket.on('error', err => setError([...error, err]))
}
useEffect(() => {
roomSocketConnect();
}, [])
// ! [end]
return (
<div className="page">

View file

@ -89,6 +89,5 @@ function onListening() {
debug('Listening on ' + bind);
}
const socketIO = require('socket.io')
const enableSocket = require('../socket').enableSocket;
const io = enableSocket(socketIO(server, { cookie: false }));
const socket = require('../socket');
socket.io.attach(server);

View file

@ -1,9 +1,11 @@
const roomQueries = require('../../data/queries/room');
const {enableRoomSocket} = require('../../socket');
const roomIndex = async (req, res, next) => {
try {
// TODO eventually add check for user's private rooms
const publicRooms = await roomQueries.findPublicRooms();
enableRoomSocket(1)
res.status(200).json({rooms: publicRooms})
}

View file

@ -1,13 +1,20 @@
// TODO const someSocketLogic = require('./middleware/socketssockets/...');
const socketIO = require('socket.io');
const io = socketIO({ cookie: false });
io.on('connection', ()=> {
io.emit('connected', {message: 'socket connected'});
})
enableRoomSocket = (roomId) => {
const roomSocket = io.of(roomId);
roomSocket.on('connection', () => {
console.log(`Socket connected at room ${roomId}`);
});
return roomSocket;
}
module.exports = {
enableSocket: io => {
io.on('connection', ()=> {
io.emit('connected', {message: 'socket connected'});
})
// TODO io = someSocketLogic(io);
return io;
}
io,
enableRoomSocket
}