stub room socket connection (client + server)
This commit is contained in:
parent
e8867b9634
commit
c001b939d1
4 changed files with 43 additions and 15 deletions
|
@ -2,9 +2,16 @@ 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 = () => {
|
||||
|
@ -36,6 +43,19 @@ const Home = props => {
|
|||
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">
|
||||
|
||||
|
|
|
@ -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);
|
|
@ -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})
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,20 @@
|
|||
// TODO const someSocketLogic = require('./middleware/socketssockets/...');
|
||||
const socketIO = require('socket.io');
|
||||
const io = socketIO({ cookie: false });
|
||||
|
||||
module.exports = {
|
||||
enableSocket: io => {
|
||||
io.on('connection', ()=> {
|
||||
io.emit('connected', {message: 'socket connected'});
|
||||
})
|
||||
|
||||
// TODO io = someSocketLogic(io);
|
||||
enableRoomSocket = (roomId) => {
|
||||
const roomSocket = io.of(roomId);
|
||||
roomSocket.on('connection', () => {
|
||||
console.log(`Socket connected at room ${roomId}`);
|
||||
});
|
||||
return roomSocket;
|
||||
}
|
||||
|
||||
return io;
|
||||
}
|
||||
module.exports = {
|
||||
io,
|
||||
enableRoomSocket
|
||||
}
|
Loading…
Reference in a new issue