add basic ws functionality

This commit is contained in:
Iuri Matias 2018-02-26 09:45:46 -05:00
parent 1ce80b3ce5
commit fd83cba4b7
3 changed files with 45 additions and 0 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

@ -4,6 +4,7 @@ let serveStatic = require('serve-static');
require('http-shutdown').extend();
var express = require('express');
let path = require('path');
var expressWebSocket = require('express-ws');
class Server {
constructor(options) {
@ -14,6 +15,7 @@ class Server {
}
start(callback) {
const self = this;
if (this.server && this.server.listening) {
this.logger.warn(__("a webserver is already running at") + " " + ("http://" + this.hostname + ":" + this.port).bold.underline.green);
if (callback) {
@ -28,6 +30,20 @@ class Server {
app.use(serve);
app.use('/backend', serveStatic(path.join(__dirname, 'backend'), {'backend': ['index.html', 'index.htm']}));
expressWebSocket(app);
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);
});
});
//app.get('/embark', function (req, res) {
// res.send('Welcome to Embark')
//});

View File

@ -44,6 +44,7 @@
"eth-lib": "^0.2.8",
"ethereumjs-wallet": "^0.6.0",
"express": "^4.16.2",
"express-ws": "^3.0.0",
"file-loader": "^1.1.5",
"finalhandler": "^1.1.1",
"follow-redirects": "^1.2.4",