forked from jgonera/socket.io-multichat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (39 loc) · 1.03 KB
/
app.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
var express = require('express');
var app = express()
var server = app.listen(7890);
var io = require('socket.io').listen(server);
app.set('view options', { layout: false })
app.get('/', function(req, res) {
res.render('index.ejs');
});
io.configure(function() {
io.set('transports', ['websocket']);
});
io.configure('production', function() {
io.set('log level', 1);
});
io.sockets.on('connection', function(socket) {
socket.on('join', function(channel, ack) {
socket.get('channel', function(err, oldChannel) {
if (oldChannel) {
socket.leave(oldChannel);
}
socket.set('channel', channel, function() {
socket.join(channel);
ack();
});
});
});
socket.on('message', function(msg, ack) {
socket.get('channel', function(err, channel) {
if (err) {
socket.emit('error', err);
} else if (channel) {
socket.broadcast.to(channel).emit('broadcast', msg);
ack();
} else {
socket.emit('error', 'no channel');
}
});
});
});