-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart-server.js
50 lines (41 loc) · 1.14 KB
/
start-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const http = require('http');
const app = require('./express-app');
const socketio = require('socket.io');
const uniqid = require('uniqid');
const port = 8080;
const webServer = http.createServer();
webServer.listen(port, ()=>{
console.log('web socker server started!');
});
webServer.on('request', app);
const io = socketio(webServer, {
cors: {
origin: '*',
}
});
io.on('connection', (socket) => {
console.log(socket.id, ' connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('joinRoom', (msg) => {
let msgObj = JSON.parse(msg);
/*
* msgObj example : {
* roomId: 'any_room_id_string'
* }
*/
if (msgObj.roomId.length > 0) {
console.log('id:', socket.id, 'request join room:', msgObj.roomId)
socket.join(msgObj.roomId);
// broadcast to everyone in the room
io.to(msgObj.roomId).emit('new user', 'a new user has joined the room');
}
});
socket.on('new message', (msg) => {
let msgObj = JSON.parse(msg);
let roomId = msgObj.roomid;
console.log('new message')
io.to(msgObj.roomId).emit('new message', msg);
});
});