mirror of https://github.com/embarklabs/embark.git
add basic ws functionality
This commit is contained in:
parent
0917acb02f
commit
7bd6b9da8f
|
@ -1 +1,29 @@
|
||||||
Welcome to Embark!
|
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>
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ const fs = require('../../core/fs');
|
||||||
require('http-shutdown').extend();
|
require('http-shutdown').extend();
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
let path = require('path');
|
let path = require('path');
|
||||||
|
var expressWebSocket = require('express-ws');
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
|
@ -64,18 +65,30 @@ class Server {
|
||||||
this.app.use('/coverage', coverage);
|
this.app.use('/coverage', coverage);
|
||||||
this.app.use(coverageStyle);
|
this.app.use(coverageStyle);
|
||||||
|
|
||||||
this.app.ws('/', (_ws, _req) => {});
|
expressWebSocket(this.app);
|
||||||
const wss = expressWs.getWss('/');
|
|
||||||
|
|
||||||
self.events.on('outputDone', () => {
|
this.app.ws('/logs', function(ws, req) {
|
||||||
wss.clients.forEach(function (client) {
|
self.events.on("log", function(logLevel, logMsg) {
|
||||||
client.send('outputDone');
|
console.dir("got message " + logMsg);
|
||||||
|
ws.send(logMsg);
|
||||||
|
});
|
||||||
|
ws.on('message', function(msg) {
|
||||||
|
console.dir("got message");
|
||||||
|
console.dir(msg);
|
||||||
|
ws.send(msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
self.events.on('outputError', () => {
|
this.app.ws('/', function(ws, _req) {
|
||||||
wss.clients.forEach(function (client) {
|
self.events.on('outputDone', () => {
|
||||||
client.send('outputError');
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,7 @@
|
||||||
"i18n": "0.8.3",
|
"i18n": "0.8.3",
|
||||||
=======
|
=======
|
||||||
"express": "^4.16.2",
|
"express": "^4.16.2",
|
||||||
|
"express-ws": "^3.0.0",
|
||||||
"file-loader": "^1.1.5",
|
"file-loader": "^1.1.5",
|
||||||
"finalhandler": "^1.1.1",
|
"finalhandler": "^1.1.1",
|
||||||
"follow-redirects": "^1.2.4",
|
"follow-redirects": "^1.2.4",
|
||||||
|
|
Loading…
Reference in New Issue