-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (38 loc) · 1.02 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 ws = require("nodejs-websocket");
console.log("Start building a connection...");
//init
var pagesConn = null,
pagesServer = null,
pagesConnReady = false,
pagesServerReady = false;
//create server
var server = ws.createServer(function(conn) {
//Messages received
conn.on("text", function(str) {
console.log("Messages received:" + str);
if (str === "connection") {
pagesConn = conn;
pagesConnReady = true;
conn.sendText("success");
}
if (str === "server") {
pagesServer = conn;
pagesServerReady = true;
}
if (pagesConnReady && pagesServerReady) {
pagesServer.sendText(str);
}
conn.sendText(str);
});
//close
conn.on("close", function(code, reason) {
console.log("Close connection");
});
//error
conn.on("error", function(code, reason) {
console.log("error");
});
}).listen(8001);
//为这两页面开个路由
//实现点击之后的内容存起来
console.log("WebSocket success");