-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (69 loc) · 2.12 KB
/
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
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
/**
*Created with JetBrains WebStorm.
*
* Date:2017/12/8
*
* AUthor: SQS
*
*/
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req,res){
fs.readFile('/',function(error,data){
res.writeHead(200,{'Content-Type':'text/html'});
res.end(data,'utf-8');
});
}).listen(3001,"127.0.0.1");
console.log('Server running at http://127.0.0.1:3001/');
var io = require('socket.io').listen(server);
io.sockets.on('connection',function(socket){
// console.log('User connected');
socket.on('hi', function(data) {
console.log(data);
//触发客户端事件c_hi
// 广播消息
//给除了自己以外的客户端广播消息
//socket.broadcast.emit("msg",{data:"hello,everyone"});
//给所有客户端广播消息
//io.sockets.emit("msg",{data:"hello,all"});
// io.sockets.socket(socketid).emit(‘String’, data);//给指定的客户端发送消息
// io.sockets.socket('1m2Ew8uWxbGdGbK8AAAO').emit('c_hi', { hello: 'world' });//给指定的客户端发送消息
if (io.sockets.connected['1m2Ew8uWxbGdGbK8AAAO']) {
io.sockets.connected['1m2Ew8uWxbGdGbK8AAAO'].emit('message','surprise');
}
// io.sockets.emit('c_hi',{ hello: 'world' }) //时间广播
});
socket.on('disconnect',function(){
console.log('User disconnected');
});
});
// var express = require('express');
// var app = express();
//
// var server = require('http').createServer(app);
// var io = require('socket.io')(server);
//
// app.use('/', express.static(__dirname + '/public'));
//
// server.listen(80);
//
// console.log(11)
// //socket部分
// io.on('connection', function(socket) {
// //接收并处理客户端的hi事件
// socket.on('hi', function(data) {
// console.log(data);
//
// //触发客户端事件c_hi
// socket.emit('c_hi','hello too!')
// })
//
// //断开事件
// socket.on('disconnect', function(data) {
// console.log('断开',data)
// socket.emit('c_leave','离开');
// //socket.broadcast用于向整个网络广播(除自己之外)
// //socket.broadcast.emit('c_leave','某某人离开了')
// })
//
// });