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 e8867b9634
commit c001b939d1
4 changed files with 43 additions and 15 deletions

View file

@ -2,9 +2,16 @@ import React, { useEffect } from 'react';
import './Home.scss'; import './Home.scss';
import roomsServices from '../../services/api/roomsServices'; import roomsServices from '../../services/api/roomsServices';
import RoomButton from '../../components/Button/Room/Room'; 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 Home = props => {
const state = props.state; const state = props.state || {};
const dispatch = props.dispatch; const dispatch = props.dispatch;
const renderRooms = () => { const renderRooms = () => {
@ -36,6 +43,19 @@ const Home = props => {
fetchRoomsAPI(); 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 ( return (
<div className="page"> <div className="page">

View file

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

View file

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

View file

@ -1,13 +1,20 @@
// TODO const someSocketLogic = require('./middleware/socketssockets/...'); // 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 = { module.exports = {
enableSocket: io => { io,
io.on('connection', ()=> { enableRoomSocket
io.emit('connected', {message: 'socket connected'});
})
// TODO io = someSocketLogic(io);
return io;
}
} }