Skip to content

Commit

Permalink
updated Node to 16
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSnowbird authored and DrSnowbird committed Jul 8, 2021
1 parent 4fcd6d3 commit d0034ba
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 107 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ node_modules
*.pem
*.crt

**/package-lock.json
**/package.json

13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ubuntu
FROM ubuntu:20.04

MAINTAINER openkbs.org@gmail.com
MAINTAINER DrSnowbird "DrSnowbird@openkbs.org"

ENV DEBIAN_FRONTEND noninteractive

Expand All @@ -21,7 +21,7 @@ COPY ./scripts ${SCRIPT_DIR}
RUN chmod +x ${SCRIPT_DIR}/*.sh

#### ---- Apt Proxy & NPM Proxy & NPM Permission setup if detected: ---- ####
RUN cd ${SCRIPT_DIR}; ${SCRIPT_DIR}/setup_system_proxy.sh
#RUN cd ${SCRIPT_DIR}; ${SCRIPT_DIR}/setup_system_proxy.sh

########################################
#### update ubuntu and Install Python 3
Expand Down Expand Up @@ -148,7 +148,7 @@ RUN mkdir -p ${GRADLE_INSTALL_BASE} && \
#### ---- Node from NODESOURCES ---- ####
#########################################
# Ref: https://github.com/nodesource/distributions
ARG NODE_VERSION=${NODE_VERSION:-15}
ARG NODE_VERSION=${NODE_VERSION:-16}
ENV NODE_VERSION=${NODE_VERSION}
RUN apt-get update -y && \
apt-get install -y sudo curl git xz-utils && \
Expand Down Expand Up @@ -210,6 +210,11 @@ COPY ./examples ${DATA}/examples
VOLUME ${DATA}
VOLUME ${WORKSPACE}

############################################
#### ---- NPM: websocket ---- ####
############################################
RUN npm install websocket ws

#########################
#### ---- Entry ---- ####
#########################
Expand Down
28 changes: 0 additions & 28 deletions docker.env

This file was deleted.

37 changes: 37 additions & 0 deletions examples/mini-echo-server.js
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');
});

});

34 changes: 34 additions & 0 deletions examples/ws-client.js
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');
52 changes: 52 additions & 0 deletions examples/ws-echo-server.js
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.');
});
});
6 changes: 3 additions & 3 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ case "${BUILD_TYPE}" in
${VOLUME_MAP} \
${PORT_MAP} \
${imageTag} \
$*
"$@"
;;
1)
#### 1: X11/Desktip container build image type
Expand All @@ -694,7 +694,7 @@ case "${BUILD_TYPE}" in
${VOLUME_MAP} \
${PORT_MAP} \
${imageTag} \
$*
"$@"
;;
2)
#### 2: VNC/noVNC container build image type
Expand All @@ -717,7 +717,7 @@ case "${BUILD_TYPE}" in
${VOLUME_MAP} \
${PORT_MAP} \
${imageTag} \
$*
"$@"
;;

esac
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup_system_certificates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ echo "####################### Components: $(basename $0) #######################
if [ "$1" != "" ]; then
    SOURCE_CERTIFICATES_DIR=${SOURCE_CERTIFICATES_DIR:-$1}
else
    SOURCE_CERTIFICATES_DIR=${SOURCE_CERTIFICATES_DIR:-/certificates}
    SOURCE_CERTIFICATES_DIR=${SOURCE_CERTIFICATES_DIR:-"/certificates"}
fi

#### ---------------------------------------------------------------------------------------------------------------------------------- ####
Expand Down
87 changes: 16 additions & 71 deletions tryWebSocketServer.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/bash
#!/bin/bash -x

SCRIPT_FILE=/home/developer/data/examples/ws-echo-server.js

SCRIPT_FILE=WebSocketServer.js
HOST_PORT=8080
SERVER_PORT=8080

TIMEOUT_SEC=40
TIMEOUT_SEC=120

###################################################
#### ---- Change this only if want to use your own
Expand All @@ -28,76 +29,20 @@ function cleanup() {
}
cleanup

mkdir -p ./data

# To run: nodejs ./WebSocketServer.js
# Then => Open http://localhost:8080/
# To see: Hello World!

cat > ./data/${SCRIPT_FILE} <<'EOF'
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');
});
});
EOF

echo
echo "----------------------------------------------------------------------"
echo "Starting JavaScript NodeJS mini-webserver:"
echo "You have $TIMEOUT_SEC to test this NodeJS mini-server."
echo "... then, it will destroy itself (like 007 Bond's movie - self-clean up :-) "
echo "----------------------------------------------------------------------"
echo "---> Open your browser to : http://127.0.0.1:${HOST_PORT}/"
echo "---> Or, command line:"
echo " curl http://localhost:${HOST_PORT}/"
echo " curl http://127.0.0.1:${HOST_PORT}/"
echo
docker run -d --rm --name ${instanceName} -v $PWD/data:/data -p ${HOST_PORT}:${SERVER_PORT} --workdir /data openkbs/jdk-mvn-py3 nodejs /data/${SCRIPT_FILE}
#if [ ! -s ./data ]; then
# mkdir -p ./data
#fi

echo "---> Testing the mini-server by the ${SCRIPT_FILE} script:"
echo "Use Web Socket Client, e.g., Chrome plugins, WebSocketTestClient, SimpleWebSocketClient in brwoser to test:"
echo "For example,"
echo " echo \"Some data to be sent\" | websocat ws://127.0.0.1:${HOST_PORT} "
echo "or,"
echo " echo \"Some data to be sent\" | python3-wsdump ws://127.0.0.1:${HOST_PORT} "
echo ""
echo "Obviously, there are also alternatives like wscat (golang) or wscat (node)."
echo "To install python3-wsdump package: sudo apt install -y python3-websocket"
echo "... You have $TIMEOUT_SEC seconds to try out the above URL provided by JavaScript as Web Servers."
sleep $TIMEOUT_SEC
echo "---> run the ws-client.js program:"
echo ".... Open an other XTERM console with the current same directory:"
echo ".... then run the command below to see the Client/Server messages"
echo " node examples/ws-client.js"
echo

cleanup
echo ".... Starting websocket echo server ....."
echo

./run.sh node ${SCRIPT_FILE}
#./run.sh node /home/developer/data/examples/ws-echo-server.js

0 comments on commit d0034ba

Please sign in to comment.