-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (125 loc) · 4.18 KB
/
Copy pathserver.js
File metadata and controls
148 lines (125 loc) · 4.18 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Internet proxy for HA Server, web server for client and proxy all web
// server websockets requests to the home server
setInterval(checkConn, 10000);
var express = require('express');
var compression = require('compression');
var url = require('url');
var app = express();
//var __dirname = "wwwroot/";
app.use(compression({
threshold: 512
}));
var oneDay = 86400000;
app.use(express.static(__dirname + "/", { maxAge: oneDay }));
console.log('Starting websocket relay');
var http = require('http').createServer(app).listen(process.env.PORT);
var wsSvr = require('ws').Server;
var wss = new wsSvr({ server: http });
var svrSock = null, cliSock = null;
wss.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
console.log("Connection from: " + location.href);
if (location.path === "/host:80/HAServer") {
console.log("Home Server connected: " + location.path);
svrSock = ws;
ws.on('message', function incoming(msg) {
if (msg !== "") {
console.log("Message from Home Server: " + msg);
if (cliSock !== null) cliSock.send(msg);
}
});
ws.on('close', function () {
console.log("Home Server disconnected");
});
} else {
console.log("Remote client connected: " + location.path);
cliSock = ws;
ws.on('message', function incoming(msg) {
console.log("Message from remote client: " + msg);
if (svrSock !== null) {
svrSock.send(msg);
console.log("Sent to Server")
} else {
console.log("Server not connected, not sent");
}
});
ws.on('close', function () {
console.log("HA Client disconnected");
if (svrSock !== null) svrSock.close();
});
}
});
function checkConn() {
console.log("running");
}
/*
wsc.on('connection', function connection(ws) {
console.log("Remote client connected: " + location.host);
cliSock = ws;
ws.on('message', function incoming(msg) {
console.log("Message from remote client: " + msg);
if (svrSock !== null) {
svrSock.send(msg);
console.log("Sent to Server")
} else {
console.log("Server not connected, not sent");
}
});
ws.on('close', function () {
console.log("HA Client disconnected");
if (svrSock !== null) svrSock.close();
});
});
function checkConn() {
console.log("running");
}
/*
// Internet proxy for HA Server, web server for client and proxy all web
// server websockets requests to the home server
var express = require('express');
var compression = require('compression');
var app = express();
//var __dirname = "wwwroot/";
app.use(compression({
threshold: 512
}));
var oneDay = 86400000;
app.use(express.static(__dirname + "/", { maxAge: oneDay }));
console.log('Starting websocket relay');
var ws = require('websocket.io');
var http = require('http').createServer(app).listen(process.env.PORT);
var homeSvr = ws.attach(http, { path: '/HAServer', clientTracking: true });
var cliSvr = ws.attach(http, { path: '/HAclient', clientTracking: true });
var svrSock = null, cliSock = null;
homeSvr.on('connection', function (socket) {
console.log("Home Server connected");
svrSock = socket;
socket.on('message', function (msg) {
console.log("Message from Home Server: " + msg);
if (cliSock !== null) cliSock.send(msg);
});
socket.on('close', function () {
console.log("Home Server disconnected");
});
});
cliSvr.on('connection', function (socket) {
console.log("HA Client connected");
cliSock = socket;
socket.on('message', function (msg) {
console.log("Message from HA Client: " + msg);
if (svrSock !== null) {
svrSock.send(msg);
console.log("Sent to Server")
} else {
console.log("Server not connected, not sent");
}
});
socket.on('close', function () {
console.log("HA Client disconnected");
if (svrSock !== null) svrSock.close();
});
});
setInterval(checkConn, 3000);
function checkConn() {
}
*/