Skip to content
Snippets Groups Projects
Commit 06394575 authored by Jake Read's avatar Jake Read
Browse files

ws connecting

parent 4ecdf4f5
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ const { ...@@ -12,6 +12,7 @@ const {
Worker, workerData Worker, workerData
} = require('worker_threads') } = require('worker_threads')
// will use these to figure where tf we are // will use these to figure where tf we are
let ownIp = ''
const os = require('os') const os = require('os')
let ifaces = os.networkInterfaces() let ifaces = os.networkInterfaces()
...@@ -82,18 +83,23 @@ app.get('/pipeHookup/:file', (req, res) => { ...@@ -82,18 +83,23 @@ app.get('/pipeHookup/:file', (req, res) => {
// we can assume that the file is a reciprocal pipe-type in our local pipes/file.js location // we can assume that the file is a reciprocal pipe-type in our local pipes/file.js location
// we'll open that can as a spawn, can assume it's hosting a websocket (it will tell us the port?) // we'll open that can as a spawn, can assume it's hosting a websocket (it will tell us the port?)
// and we can send that information back up stream, // and we can send that information back up stream,
console.log('/pipes', req.params.file) // every time we get one of these, we should find & nuke any of the same name
// to restart,
// that way, remote clients can attempt a reset by issuing the same request
// and report server-down if nothing happens
const piper = new Worker(`${__dirname}/pipes/${req.params.file}`) const piper = new Worker(`${__dirname}/pipes/${req.params.file}`)
piper.on('message', (msg) => { piper.on('message', (msg) => {
console.log('worker msg', msg) if(msg.startup){
console.log('worker for /pipeHookup/' + req.params.file + ' resolves')
res.send({startup: true, ip: ownIp, port: msg.port})
}
}) })
piper.on('error', (err) => { piper.on('error', (err) => {
console.log('worker err', err) console.log('worker err', err)
}) })
piper.on('exit', (code) => { piper.on('exit', (code) => {
console.log('exit code', code) console.log('worker exit code', code)
}) })
res.send({address: 'localip', port: '1024'})
// then this (or similar) should be all we really need to add here, and we can do local-dev of the pipes in hunks/pipes/pipename.js and pipes/pipename.js // then this (or similar) should be all we really need to add here, and we can do local-dev of the pipes in hunks/pipes/pipename.js and pipes/pipename.js
// so, do we spawn, or do we use workers ? // so, do we spawn, or do we use workers ?
// awh yis it's workers, messages easy, errors also OK to catch... nextup: draw the sys, what wraps what doesn't? mostly: want to be able to refresh / reload / restart remotely // awh yis it's workers, messages easy, errors also OK to catch... nextup: draw the sys, what wraps what doesn't? mostly: want to be able to refresh / reload / restart remotely
...@@ -112,6 +118,7 @@ Object.keys(ifaces).forEach(function(ifname) { ...@@ -112,6 +118,7 @@ Object.keys(ifaces).forEach(function(ifname) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return; return;
} }
ownIp = iface.address
if (alias >= 1) { if (alias >= 1) {
console.log('cf available on: \t' /*ifname + ':' + alias,*/ + iface.address + `:${port}`); console.log('cf available on: \t' /*ifname + ':' + alias,*/ + iface.address + `:${port}`);
// this single interface has multiple ipv4 addresses // this single interface has multiple ipv4 addresses
......
...@@ -22,9 +22,32 @@ export default function Pipe() { ...@@ -22,9 +22,32 @@ export default function Pipe() {
// coming merge of init and onload, however: // coming merge of init and onload, however:
this.init = () => { this.init = () => {
startWsConnection()
}
let ws = {}
let startWsConnection = () => {
// hijack ajax to ask for a websocket // hijack ajax to ask for a websocket
jQuery.get('pipeHookup/pipetemplate.js', (data) => { jQuery.get('pipeHookup/pipetemplate.js', (data) => {
console.log('pipe jquery data', data) if (data.startup) {
console.log('serverside launched, starting client')
console.log(data)
// have data.ip and data.port
ws = new WebSocket(`ws://${data.ip}:${data.port}`)
ws.onopen = (evt) => {
console.log('vfpt opens', evt)
}
ws.onerror = (evt) => {
console.log('vfpt error', evt)
}
ws.onclose = (evt) => {
console.log('vfpt close', evt)
}
ws.onmessage = (msg) => {
console.log('vfpt recv', msg)
}
}
}) })
} }
......
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
}, },
"async-limiter": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
},
"body-parser": { "body-parser": {
"version": "1.19.0", "version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
...@@ -367,6 +372,14 @@ ...@@ -367,6 +372,14 @@
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
},
"ws": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.2.tgz",
"integrity": "sha512-gftXq3XI81cJCgkUiAVixA0raD9IVmXqsylCrjRygw4+UOOGzPoxnQ6r/CnVL9i+mDncJo94tSkyrtuuQVBmrg==",
"requires": {
"async-limiter": "^1.0.0"
}
} }
} }
} }
...@@ -2,4 +2,17 @@ const { ...@@ -2,4 +2,17 @@ const {
parentPort parentPort
} = require('worker_threads') } = require('worker_threads')
parentPort.postMessage('hello worker') const WebSocketServer = require('ws').Server
let port = 2042
const WSS = new WebSocketServer({port: port}, () => {
parentPort.postMessage({
startup: true,
port: port
})
})
WSS.on('connection', (ws) => {
console.log('ws connects')
// ...
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment