node.js - Socket.IO server not receiving message from client -


i'm playing around node, socket.io , bdd creating chat application. during 1 of tests, timeout error stating:

error: timeout of 2000ms exceeded. ensure done() callback being called in test.

the affected test is

it('#must able receive message', function(done) {     chatterserver.on('chattermessage', function(data)     {         console.log('incoming message!');         expect(data).to.have.property('message');         expect(data.message).to.be('hello, world!');         done();     });      console.log('sending message!');     chatterclient.send('chattermessage', { message: 'hello, world!' });     console.log('sent!'); }); 

i found cause of issue chattermessage event not being caught server. whilst did specify it.

the console's output is:

sending message!
sent!
error: timeout of 2000ms exceeded. ensure done() callback being called in test.

i'm doing wrong. i'm not familiar node , socket.io, i'm sorry if question obvious.

i looked around google search terms 'socket.io server not receiving client', found, nothing helped me solve issue far.

i did try solution in this question, didn't fix me.

i'm using mocha , expect.js

the complete test is:

var util = require('util'); var chatter = require('../src/index'); var chatterserver = chatter.server; var chatterclient = chatter.client; var express = require('express'); var expect = require('expect.js'); var socketio = require('socket.io'); var socketioclient = require('socket.io-client');  var host = 'http://localhost'; var port = 8080;  describe('chatter', function() {     'use strict';      var chatterserver;     var chatterclient;     var server;      before(function()     {         var app = express();          server = app.listen(port);     });      beforeeach(function()     {         chatterserver = new chatterserver(socketio(server));         chatterclient = new chatterclient(socketioclient, util.format('%s:%s', host, port.tostring()));     });      ...      it('#must able receive message', function(done)     {         chatterserver.on('chattermessage', function(data)         {             console.log('incoming message!');             expect(data).to.have.property('message');             expect(data.message).to.be('hello, world!');             done();         });          console.log('sending message!');         chatterclient.send('chattermessage', { message: 'hello, world!' });         console.log('sent!');     }); }); 

my client (chatterclient) is:

(function() {     'use strict';      function client(socketio, url)     {         this.socketio = socketio(url);     }      client.prototype.send = function(event, data)     {         this.socketio.emit(event, data);     };      client.prototype.on = function(event, callback)     {         this.socketio.on(event, callback);     };      if (module !== undefined && module.hasownproperty('exports')) {         module.exports = client;     } else {         window.chatter = {             client: client,         };     } }()); 

the server (chatterserver) is:

(function() {     'use strict';      function server(socketio)     {         this.socketio = socketio;         this.connectedusers = {};          this.on('connection', (function(user)         {             var userid = user.client.id;              this.connectedusers[userid] = user;              user.emit('chatterconnectionacknowledged', { id: userid });         }).bind(this));     }      server.prototype.on = function(event, handler)     {         this.socketio.on(event, handler);     };      module.exports = server;  }()); 

you need change code in 2 sides.

first side, need listen incoming socket connections on socketio object. (see emphasized code below)

     //.. code      function server(socketio)     {         this.socketio = socketio;         this.connectedusers = {};          this.socketio.on('connection', (function(user)         {             var userid = user.client.id;              this.connectedusers[userid] = user;              user.emit('chatterconnectionacknowledged', { id: userid });         }).bind(this));     }      //.. code 

second side, when adding new events listen on server, need bind events sockets since ones going listen when events emitted socket clients.

server.prototype.on = function (event, handler) {   object.keys(this.connectedusers).map(function (key) {     this.connectedusers[key].on(event, handler);   }.bind(this)); }; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -