1

the issue that occurs here, is that, when i connect between sample socekt.io client with this socket.io server by node.js ( just running two terminals and opening socket connection between client and server) I have no problems.

But, when I am trying to inject this socket.io-client into my Ember.js application, precisely to ember-cli-mirage it redirects my client from given address :
( 'http: //localhost:8080')
to something like
http: //localhost:8080/socket.io/?EIO=3&transport=polling&.....
also Mirage displays me an error that I cannot handle, even by setting up precise namespace, routing the wsClient.connect() method or calling this.passthrough() , before calling wsClient.connect() .


I also paste the the screenshot of error from inspect console in browser:

error image


Do you have any idea how to resolve this problem? Thank you in advance and I also hope that the topic is not duplicated.

// server.js
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
app.listen(8080);

function handler(req, res) {
  res.writeHead(200);
  res.end('default.index');
}

var rooms = {
  'room1': [

  ],
  'room2': [

  ]
};

io.on('connection', function(socket) {
  console.log('client connected');


  socket.on('join', function(roomName) {
    rooms[roomName].push(socket.id);
    socket.join(roomName);
  });

  socket.on('leave', function(roomName) {
    var toRemove = rooms[roomName].indexOf(socket.id);
    rooms[roomName].splice(toRemove, 1);
    socket.leave('roomName');
  });


  socket.on('eNotification', function(data) {
    console.log(data);
    io.to(socket.id).emit('eNotificationCall', data);
    io.to('room2').emit('eventNotification', data);
  });


  socket.on('gNotification', function(data) {
    console.log(data);
    io.to(socket.id).emit('gNotificationCall', data);
    io.to('room1').emit('diagram1Notification', data);
  });


  socket.on('close', function() {
    console.log('client disconnected');
  });

});


//client.js
var wsClient = {
  socket: null,
  connect: function() {
    this.socket = io.connect('http://localhost:8080');
    this.socket.on('connect', function() {

      console.log('mirage client connected!');
    });

  },
  send: function(eventData, graphData) {
    this.socket.emit('eNotification', eventData);
    this.socket.emit('gNotification', graphData);
  }

};
export default wsClient;



//config.js
import wsClient from './websockets/client';

export default function() {
wsClient.connect();
console.log(wsClient.socket);
var graphData = {X: "2", Y: "3"};
var eventData = {myDAta: 'myDAta', message: 'message'};
setInterval(function() {
    wsClient.send(graphData, eventData);
}, 5000);
}
gnerkus
  • 11,357
  • 6
  • 47
  • 71

1 Answers1

2

If you call this.passthrough() with no args it only allows requests on the current domain to passthrough. It looks like the websocket connection is on a different port, so try specifying it directly:

this.passthrough('http://localhost:8080/**');

See the docs for more information.

Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104
  • Thank you for your answer, but i still have a problem. I can connect to my server, but this connection is not stable - it disconnects as fast as it connects to the server. It looks like mirage would call my connect method on the loop. Still do not know how to resolve it, do you have any idea? – tekobylinski Apr 23 '16 at 14:59
  • Can you post a snapshot of the error? Also feel free to join #ec-mirage in Ember's slack – Sam Selikoff Apr 24 '16 at 18:57
  • Sorry for wasting your time, my friend found solution to this problem, which I have missed - [topic with solution](http://stackoverflow.com/questions/28238628/socket-io-1-x-use-websockets-only) . Thank you for your help again! – tekobylinski Apr 28 '16 at 19:59