stub room socket connection (client + server)
This commit is contained in:
parent
603201d45c
commit
5867c0ddd9
4 changed files with 43 additions and 15 deletions
|
@ -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">
|
||||||
|
|
||||||
|
|
|
@ -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 }));
|
|
|
@ -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})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 });
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
enableSocket: io => {
|
|
||||||
io.on('connection', ()=> {
|
io.on('connection', ()=> {
|
||||||
io.emit('connected', {message: 'socket connected'});
|
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