0

I am trying to create a dummy websocket server in javascript to send some message to my android client app. The messages will be injected to the server using a html page( javascript ), which will further be passed on to the android client. I am able to connect these two clients (web and android) individually with the server, however, unable to achieve the flow I want, i.e. Web based javascript sends message to running Nodejs websocket server, which broadcast this message to the android client.

This is the code I am using for server side

var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var port = 2001;

var app = express();
app.use(express.static(__dirname + "/../"));
app.get('/someGetRequest', function(req, res, next) {
  console.log('receiving get request');
});
app.post('/somePostRequest', function(req, res, next) {
  console.log('receiving post request');
});
app.listen(80); //port 80 need to run as root

console.log("app listening on %d ", 80);

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

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

var userId;
var wss = new WebSocketServer({
  server: server
});
wss.on("connection", function(ws) {
  console.info("websocket connection open");

  var timestamp = new Date().getTime();
  userId = timestamp;

  ws.send(JSON.stringify({
    msgType: "onOpenConnection",
    msg: {
      connectionId: timestamp
    }
  }));


  ws.on("message", function(data, flags) {
    console.log("websocket received a message");
    var clientMsg = data;

    ws.send(JSON.stringify({
      msg: {
        connectionId: userId
      }
    }));
    console.log(clientMsg);

  });

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

WebClient:

< script type = "text/javascript" >
  var websocketURL = 'ws://localhost:2001/';

function startWebSocket() {
  try {
    ws = new WebSocket(websocketURL);
  } catch (e) {
    alert("Unable to connect to webserver")
  }
}

function sendMessage(text) {
  var message = 'Test message from webclient: ' + text;
  ws.send(message);
  alert(message);
}

startWebSocket(); < /script>
        
        <button onclick="sendMessage('From button1')">Button 1</button > < br >
  < button onclick = "sendMessage('From button2')" > Button 2 < /button><br>

Android client:

Just using socket class and its method to do further processing

               s = new Socket(HOST, TCP_PORT);

Please let me know how I can pass the message generated from the web client to my android client via websocket server.

I am using nodejs for websocket server implementation.

Thanks

blackbug
  • 1,098
  • 3
  • 13
  • 40

2 Answers2

0

From https://datatracker.ietf.org/doc/html/draft-hixie-thewebsocketprotocol-76

The protocol consists of an initial handshake followed by basic message framing, layered over TCP.

So, just opening a Socket on the client side isn't enough. Maybe this will help https://stackoverflow.com/a/4292671

Also take a look at http:// www.elabs.se/blog/66-using-websockets-in-native-ios-and-android-apps chapter Android client

If you really want to implement the WebSocket stuff yourself, take a look at https://stackoverflow.com/a/8125509 and https://www.rfc-editor.org/rfc/rfc6455

Community
  • 1
  • 1
0

I guess I misread your question. Since the connection between the clients and the server already works, you just need to forward the messages.

First, you need to identify the WebSocket client type (Android or Web). Meaning, you immediately send a message what type of client the newly opened WebSocket connection is and store the WebSocket (ws) for that type in the server. Since you have identified and stored each WebSocket connection, you just forward the message to the other type.

For a more specific answer, I need more information.

Should the communication be bidirectional?
Should there be multiple web and Android connections at the same time?