HenningM / express-ws

WebSocket endpoints for express applications
BSD 2-Clause "Simplified" License
877 stars 142 forks source link

Not seeing callback in Node.js when browser sends a message #29

Open nkolban opened 8 years ago

nkolban commented 8 years ago

I am running a browser side script which sends a message to Node.js running express hosting express-ws. I am seeing the connection form fine but when I send a message from the browser, I am not seeing any callbacks on the server side.

Here is my browser code:

$(function() {
  var ws = new WebSocket("ws://raspi3:3000/test");

  ws.onopen = function() {
    console.log("Web Socket open!");
    ws.send("Hi!");
  };
  ws.onerror = function(err) {
    console.log("Web socket error: " + err);
  };
  ws.onclose = function() {
    console.log("Web socket closing: ");
  };
});

and here is my server code:

var express = require("express");

var app = express();
var expressWs = require("express-ws")(app);

app.ws("/test", function(s1, req) {
  conosle.log("ws handler called");
  s1.on("message", function(msg) {
    console.log("We got a ws message!");
    s1.send("Hi!");
  });
});

app.use(express.static("."));

app.listen(3000, function() {
  console.log("Express app started on port 3000");
});

I am anticipating the console.log messages in app.ws() to appear ... but I am not seeing any of them.

AgustinCB commented 8 years ago

Same problem here.

roastduck commented 8 years ago

You misspelled "console" into "conosle" in line 7 of the server code, which throws an exception and what follows doesn't run.

roastduck commented 8 years ago

However, I haven't figured out why the exception doesn't prompt out in the console. Anything catches the exception?

ilovepumpkin commented 6 years ago

I saw the same. I added "onclose" and "onopen" logging in browser and found right after '** ws connected' is printed out, 'Websocket is closed.' is printed out. There is no any exception either at browser side or server side.

    ws.onopen = function() {
      console.log('****** ws connected');
      ws.send('helloworld');
    };

    ws.onclose = function() {
      console.log('Websocket is closed.');
    };