aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-18 22:33:00 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-18 22:33:00 +0100
commit97d38dd4ee9ae37c3c307f601dfee0f8b3b6437f (patch)
treec1af1a3806aebdd2925b8ba066444b688b80769c
parent012f9e4156919157bfff0d5ce8f105a04b0c4a70 (diff)
Add websocket translation script
-rw-r--r--websockets/.gitignore1
-rw-r--r--websockets/package.json22
-rwxr-xr-xwebsockets/server.js45
3 files changed, 68 insertions, 0 deletions
diff --git a/websockets/.gitignore b/websockets/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/websockets/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/websockets/package.json b/websockets/package.json
new file mode 100644
index 0000000..7a71e7d
--- /dev/null
+++ b/websockets/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "tomsg-webserver",
+ "version": "0.1.0",
+ "description": "tomsg websocket-to-tcp converter",
+ "main": "server.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://git.tomsmeding.com/tomsg"
+ },
+ "keywords": [
+ "tomsg",
+ "webserver"
+ ],
+ "author": "Tom Smeding <tom.smeding@gmail.com> (https://tomsmeding.com)",
+ "license": "MIT",
+ "dependencies": {
+ "ws": "^2.2.1"
+ }
+}
diff --git a/websockets/server.js b/websockets/server.js
new file mode 100755
index 0000000..c6af67f
--- /dev/null
+++ b/websockets/server.js
@@ -0,0 +1,45 @@
+#!/usr/bin/env node
+const net=require("net");
+const WebSocket=require("ws");
+
+const PORT=29546;
+
+const upstream={
+ host: "localhost",
+ port: 29536
+};
+
+
+const server=new WebSocket.Server({port:PORT},()=>{
+ console.log(`Listening for websocket http requests on port ${PORT}`);
+});
+server.on("connection",(sock)=>{
+ let netconn=null;
+ let buffer=[];
+
+ netconn=net.connect(upstream.port,upstream.host,()=>{
+ for(const item of buffer){
+ netconn.write(item);
+ }
+ buffer=[];
+ });
+ netconn.on("close",()=>{
+ sock.close();
+ });
+ netconn.on("data",(data)=>{
+ sock.send(data);
+ });
+
+ sock.on("close",()=>{
+ netconn.end();
+ });
+ sock.on("message",(data)=>{
+ if(netconn.connecting)buffer.push(data);
+ else netconn.write(data);
+ });
+});
+
+process.on("SIGINT",()=>{
+ console.log("Closing websocket server...");
+ server.close();
+});