-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (71 loc) · 2.21 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
var util = require('util');
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + req.url,
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var Room=io.sockets.on('connection', function (socket) {
// while(1)
socket.on('my other event', function (data) {
console.log(data);
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
socket.broadcast.emit('news', { hello: name+data["my"] });
});
});
socket.on('disconnect', function () {
io.sockets.emit('user disconnected');
});
socket.on('set nickname', function (name) {
socket.set('nickname', name, function () {
socket.broadcast.to(joinedRoom).send(name+' joined room');
//socket.emit('ready');
console.log(name);
});
});
socket.on('msg', function () {
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
});
});
// socket.join("ROOM1");
// console.log(util.inspect(Room.manager.roomClients, true, null));
// console.log(util.inspect(Room.manager.rooms, true, null));
var joinedRoom = null;
socket.on('join room', function(data) {
socket.join(data);
joinedRoom = data;
socket.emit('joined', "you've joined " + data);
});
socket.on('fromclient', function(data) {
if (joinedRoom) {
socket.broadcast.to(joinedRoom).send(data);
} else {
socket.send(
"you're not joined a room." +
"select a room and then push join."
);
}
});
socket.on('message', function(data) {
console.log("Client data: " + data);
// lookup room and broadcast to that room
socket.get('room', function(err, room) {
//room example - https://github.com/learnboost/socket.io
// neither method works for me
// socket.broadcast.to(room).emit('new fan');
// io.sockets.in(room).emit('new non-fan');
io.sockets.in(room).emit('message', data);
})
});
});