Tuesday, April 5, 2016

Server to server Socket IO messaging

Leave a Comment

Context:

  1. Node.js running a server with require("socket.io");
  2. Node.js running another server with require("socket.io-client");
  3. Receiving acknowledgements when using the standard :

io.on("connection", function() { console.log("Something connected."); });

Problem:

Unfortunately, I cannot get the CLIENT on either server both are running client & server socket.io to emit a message to the server of the other.

I realize it's probably a code error, so I'll include that for correctness' sake, but for the record I'm wondering if there's something I'm doing wrong semantically as opposed to code-wise.

Code: (LoginServer.js)

var app = require('http').createServer(); var io = require('socket.io')(app); var ioc = require("socket.io-client");  var LoginServer = (function() {     function LoginServer() {         this.gameServerSocket = ioc.connect("http://localhost:3031", {reconnect: true});     }      LoginServer.prototype.initialize = function() {         var self = this;          io.listen(3032);         io.on("connection", function(socket) {             socket.emit("connection");         });          this.gameServerSocket.on("connection", function() {             console.log("Game Server Connected!");             self.gameServerSocket.emit("login", {accountName: "default"});         });     }      return LoginServer; })();  exports.LoginServer = LoginServer; 

Code: (GameServer.js)

var app = require('http').createServer(); var io = require('socket.io')(app); var ioc = require("socket.io-client");  var GameServer = (function() {     function GameServer() {         this.loginServerSocket = ioc.connect("http://localhost:3032", {reconnect: true});         this.loginServerSocket.emit("connection");     }      GameServer.prototype.initialize = function() {         var self = this;          io.listen(3031);         io.on("connection", function(socket) {             socket.emit("connection");         });          this.LoginServerResponses();     }      GameServer.prototype.LoginServerResponses = function() {         var self = this;         this.loginServerSocket.on("connection", function() {             console.log("Login Server Connected!");         });         this.loginServerSocket.on("login", function(data) {             console.log("Login Server:", data);         });     }      return GameServer; })();  exports.GameServer = GameServer; 

3 Answers

Answers 1

"Unfortunately, I cannot get the CLIENT on either server both are running client & server socket.io to emit a message to the server of the other."

What you really want to do is 1 server, and allow clients to connect.

The server can communicate to the clients, so does the clients to the server, back and forth.

server.js

var app = require('http').createServer(); var io = require('socket.io')(app);  io.listen(3032); io.on('connection', function(socket) {     console.log('A user(a client) connected!');      // lets welcome this particular client with a nice msg :)     socket.emit('msg group', 'Welcome client! you can emit msgs for me to tell every client in "to everyone please"');      // lets tell everyone someone joined     io.emit('msg group', 'guys someone special joined :)!');      socket.on('to everyone please', function(freedomMsg) {         io.emit('msg group', "a client told me (the server) to send this to everyone (every clients)!: " + freedomMsg);     }); }); 

aClient.js

var ioc = require("socket.io-client"); var socket = ioc("http://localhost:3032"); // general chat socket.on('msg group', function(msg) {     console.log(msg); }); function msgEveryone(message) {     socket.emit('to everyone please', message); }  msgEveryone('whats up guys?'); 

I hope that with this you'll understand a bit more on how socket.io (or sockets in general) works :)

Answers 2

Code: (GameServer.js)

first delete these code :

this.loginServerSocket.on("login", function(data) {     console.log("Login Server:", data); }); 

second change code:

GameServer.prototype.initialize = function() {     var self = this;      io.listen(3031);     io.on("connection", function(socket) {         socket.emit("connection");     });      this.LoginServerResponses(); } 

to

GameServer.prototype.initialize = function () {     var self = this;      io.listen(3031);     io.on("connection", function (socket) {         socket.emit("connection");         socket.on("login", function (data) {             console.log("Login Server:", data);         });     });      this.LoginServerResponses(); } 

Answers 3

Try this: LoginServer.js

var app = require('http').createServer(); var io = require('socket.io')(app); var ioc = require("socket.io-client");  var LoginServer = (function() { function LoginServer() {     this.gameServerSocket = ioc.connect("http://localhost:3031", {reconnect: true}); }  LoginServer.prototype.initialize = function() {     var self = this;      io.listen(3032);     io.on("connection", function(socket) {         // a client, maybe game server, is connected         socket.on("login", function(data){ console.log("Credentials: "+ data); });     });      this.gameServerSocket.on("connect", function() {         console.log("Connected to Game Server!");         // Send event to game server     }); }      return LoginServer; })();  exports.LoginServer = LoginServer; 

GameServer.js

var app = require('http').createServer(); var io = require('socket.io')(app); var ioc = require("socket.io-client");  var GameServer = (function() { function GameServer() {     this.loginServerSocket = ioc.connect("http://localhost:3032", {reconnect: true}); }  GameServer.prototype.initialize = function() {     var self = this;      io.listen(3031);     io.on("connection", function(socket) {         // a client, maybe login server, is connected     });      this.LoginServerResponses(); }  GameServer.prototype.LoginServerResponses = function() {     var self = this;     this.loginServerSocket.on("connect", function() {         console.log("Connected to Login Server !");         // Try to login         self.loginServerSocket.emit("login", {accountName: "default"});     }); }  return GameServer; })();  exports.GameServer = GameServer; 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment