Simple nodejs socket server

I am trying to create a simple node.js server that will allow my iOS app on juice to send GPS coordinates to the server, and the server will broadcast GPS coordinates to all connected iOS clients. Likewise, clients connect to the server using sockets. I tried using example code from Heroku webserver. CODE HAS INCLUDED ANURAG ANSWER

var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000

app.use(express.static(__dirname + "/"))

var server = http.createServer(app)
server.listen(port)

console.log("http server listening on %d", port)

var wss = new WebSocketServer({server: server})
console.log("websocket server created")

var connectionList = [];

wss.on("connection", function(ws) {
    console.log("connection");
  connectionList.push(ws);
})

wss.on("message", function(data, id) {
    var mes = server.unmaskMessage(data);
    var str = server.convertToString(mes.message);
    console.log(str);
    var i;
    for(i = 0; i < connectionList.lenth; i++) {
         wss.sendMessage(one, str, connectionList[i]);
    }
});

      

How do I modify this code to receive messages from my app (via sockets) and then send that message to all connected other iOS clients. (The message is just a simple line)

BONUS QUESTION: Because Heroku forces you to use the environment port (and not your own), in my iOS app, when I connect to the server, I would just specify the port that is printed to the console when the server starts up.

Any help is appreciated, thanks! :)

EDIT: For broadcast to clients, the code is:

wss.on('connection', function(ws) {
  ws.on('message', function(message) {
    wss.broadcast(message);
  });
});

      

How can I receive messages from a client and how can I make a received message to be forwarded to other clients.

+3


source to share


2 answers


When you get a connection, you need to keep those connections. Then you can send messages to all these devices connecting to your server using these connections. You can try something like this:

var connectionList = [];
wss.on("connection", function(ws) {
  connectionList.push(ws);
})

wss.on("message", function(data, id) {
    var mes = server.unmaskMessage(data);
    var str = server.convertToString(mes.message);
    console.log(str);
    var i;
    for(i = 0; i < connectionList.lenth; i++) {
         wss.sendMessage(one, str, connectionList[i]);
    }
});

      



More details here: https://www.npmjs.com/package/websocketserver

+2


source


Here is the complete index.js code as the brodcast server received messages to clients:



var WebSocketServer = require("ws").Server
var http = require("http")
var express = require("express")
var app = express()
var port = process.env.PORT || 5000

app.use(express.static(__dirname + "/"))

var server = http.createServer(app)
server.listen(port)

console.log("http server listening on %d", port)

var wss = new WebSocketServer({server: server})
console.log("websocket server created")

wss.broadcast = function(data) {
  for (var i in this.clients)
    this.clients[i].send(data);
};


wss.on("connection", function(ws) {

  console.log("websocket connection open");

  ws.on('message', function(message) {
    console.log("message received by server");
    wss.broadcast(message);
    })

  ws.on("close", function() {
    console.log("websocket connection close")
  })
})

      

+1


source







All Articles