add basic ws functionality

This commit is contained in:
Iuri Matias 2018-02-26 09:45:46 -05:00 committed by Pascal Precht
parent 0917acb02f
commit 7bd6b9da8f
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 50 additions and 8 deletions

View File

@ -1 +1,29 @@
Welcome to Embark!
<script>
var ws = new WebSocket("ws://localhost:8000/logs");
ws.onopen = function() {
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
console.log(received_msg);
//alert("Message is received...");
};
ws.onclose = function() {
// websocket is closed.
alert("Connection is closed...");
};
window.onbeforeunload = function(event) {
socket.close();
};
</script>

View File

@ -7,6 +7,7 @@ const fs = require('../../core/fs');
require('http-shutdown').extend();
var express = require('express');
let path = require('path');
var expressWebSocket = require('express-ws');
class Server {
constructor(options) {
@ -64,19 +65,31 @@ class Server {
this.app.use('/coverage', coverage);
this.app.use(coverageStyle);
this.app.ws('/', (_ws, _req) => {});
const wss = expressWs.getWss('/');
expressWebSocket(this.app);
this.app.ws('/logs', function(ws, req) {
self.events.on("log", function(logLevel, logMsg) {
console.dir("got message " + logMsg);
ws.send(logMsg);
});
ws.on('message', function(msg) {
console.dir("got message");
console.dir(msg);
ws.send(msg);
});
});
this.app.ws('/', function(ws, _req) {
self.events.on('outputDone', () => {
wss.clients.forEach(function (client) {
client.send('outputDone');
if (ws.readyState === WEB_SOCKET_STATE_OPEN) {
return ws.send('outputDone');
}
// if the socket wasn't yet opened, listen for the 'open' event,
// then send the 'outputDone' data
ws.addEventListener('open', _event => {
ws.send('outputDone');
});
});
self.events.on('outputError', () => {
wss.clients.forEach(function (client) {
client.send('outputError');
});
});
async.waterfall([

View File

@ -74,6 +74,7 @@
"i18n": "0.8.3",
=======
"express": "^4.16.2",
"express-ws": "^3.0.0",
"file-loader": "^1.1.5",
"finalhandler": "^1.1.1",
"follow-redirects": "^1.2.4",