-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DrSnowbird
authored and
DrSnowbird
committed
Jul 8, 2021
1 parent
4fcd6d3
commit d0034ba
Showing
9 changed files
with
155 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ node_modules | |
*.pem | ||
*.crt | ||
|
||
**/package-lock.json | ||
**/package.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env node | ||
|
||
const WebSocketServer = require('ws').Server; | ||
const wss = new WebSocketServer({ port: 8080, host : '0.0.0.0' }); | ||
|
||
// -- connection -- | ||
wss.on('connection', function connection(ws) { | ||
const ip = ws._socket.remoteAddress; | ||
console.log('Server WebSocket was connected from: ' + ip); | ||
ws.send('... Hello World from openkbs/jdk-mvn-py3 with WebSocket in NodeJS supports! \n... See https://github.com/DrSnowbird/jdk-mvn-py3 for more information.\n'); | ||
|
||
// -- open -- | ||
ws.on('open', function open() { | ||
console.log('connected'); | ||
ws.send('time (open): ' + Date.now()); | ||
}); | ||
|
||
// -- message -- | ||
ws.on('message', function incoming(data) { | ||
// console.log(`Roundtrip time: ${Date.now() - data} ms`); | ||
ws.send('message: ' + data + ', received by Server at:' + Date.now()); | ||
console.log('message: ' + data + ', received by Server at:' + Date.now()); | ||
|
||
// setTimeout(function timeout() { | ||
// console.log('setTimeout at:' + Date.now()); | ||
// ws.send('setTimeout at: ' + Date.now()); | ||
// }, 500); | ||
}); | ||
|
||
// -- close -- | ||
ws.on('close', function close() { | ||
console.log('time (close) at:' + Date.now()); | ||
console.log('disconnected'); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env node | ||
var WebSocketClient = require('websocket').client; | ||
|
||
var client = new WebSocketClient(); | ||
|
||
client.on('connectFailed', function(error) { | ||
console.log('Connect Error: ' + error.toString()); | ||
}); | ||
|
||
client.on('connect', function(connection) { | ||
console.log('WebSocket Client Connected'); | ||
connection.on('error', function(error) { | ||
console.log("Connection Error: " + error.toString()); | ||
}); | ||
connection.on('close', function() { | ||
console.log('echo-protocol Connection Closed'); | ||
}); | ||
connection.on('message', function(message) { | ||
if (message.type === 'utf8') { | ||
console.log("Received: '" + message.utf8Data + "'"); | ||
} | ||
}); | ||
|
||
function sendNumber() { | ||
if (connection.connected) { | ||
var number = Math.round(Math.random() * 0xFFFFFF); | ||
connection.sendUTF(number.toString()); | ||
setTimeout(sendNumber, 1000); | ||
} | ||
} | ||
sendNumber(); | ||
}); | ||
|
||
client.connect('ws://localhost:8080/', 'echo-protocol'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env node | ||
var WebSocketServer = require('websocket').server; | ||
var http = require('http'); | ||
|
||
var server = http.createServer(function(request, response) { | ||
console.log((new Date()) + ' Received request for ' + request.url); | ||
response.writeHead(404); | ||
response.end(); | ||
}); | ||
server.listen(8080, function() { | ||
console.log((new Date()) + ' Server is listening on port 8080'); | ||
}); | ||
|
||
wsServer = new WebSocketServer({ | ||
httpServer: server, | ||
// You should not use autoAcceptConnections for production | ||
// applications, as it defeats all standard cross-origin protection | ||
// facilities built into the protocol and the browser. You should | ||
// *always* verify the connection's origin and decide whether or not | ||
// to accept it. | ||
autoAcceptConnections: false | ||
}); | ||
|
||
function originIsAllowed(origin) { | ||
// put logic here to detect whether the specified origin is allowed. | ||
return true; | ||
} | ||
|
||
wsServer.on('request', function(request) { | ||
if (!originIsAllowed(request.origin)) { | ||
// Make sure we only accept requests from an allowed origin | ||
request.reject(); | ||
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); | ||
return; | ||
} | ||
|
||
var connection = request.accept('echo-protocol', request.origin); | ||
console.log((new Date()) + ' Connection accepted.'); | ||
connection.on('message', function(message) { | ||
if (message.type === 'utf8') { | ||
console.log('Received Message: ' + message.utf8Data); | ||
connection.sendUTF(message.utf8Data); | ||
} | ||
else if (message.type === 'binary') { | ||
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); | ||
connection.sendBytes(message.binaryData); | ||
} | ||
}); | ||
connection.on('close', function(reasonCode, description) { | ||
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters