-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.js
60 lines (51 loc) · 1.74 KB
/
websocket.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
/**
* @Title
* @Description
* @Author Winnie.Cai
* @CreteData 2017/7/6.
*/
var http = require("http");
var url = require("url");
var fs = require("fs");
var server = http.createServer(function (request, response) {
var requestUrl = request.url;
if (/\w+\.(html|js)/.test(requestUrl)) {
response.writeHead(200, {'Content-Type': 'text/plain'});
fs.readFile("." + requestUrl, function (err, data) {
if (err) {
console.log(err);
// HTTP 状态码: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
} else {
// HTTP 状态码: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'});
// 响应文件内容
response.write(data.toString());
}
// 发送响应数据
response.end();
});
} else {
response.writeHead(404);
}
});
server.listen(8181);
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8080});
wss.on('connection', function (ws) {
ws.on('message', function (jsonStr) {
//保存数据
var user = this.user = JSON.parse(jsonStr);
//遍历所有客户端,发送全局消息
wss.clients.forEach(function each(client) {
client.send(JSON.stringify(user.nickname + ": " + user.msg));
});
});
ws.on('close', function (close) {
console.log('剩余数量:' + wss.clients.size);
});
console.log("当前总数量:" + wss.clients.size);
ws.send(JSON.stringify('Connection Success. Count:' + wss.clients.size));
});