Kans
Kans

Reputation: 61

nodejs not sending websocket to browser

i made a program that connects my java programm who sends data to my nodejs server using sockets and the nodejs server is supposed to send the received data to the browser using socket.io but there is a problem i do receive the data from java but the node server doesnt send it to the browser here is the code

// Create an instance of the Server and waits for a connexion
net.createServer(function(sock) {
    // Receives a connection - a socket object is associated to the connection automatically
    console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);



    // Add a 'data' - "event handler" in this socket instance
    sock.on('data', function(data) {

        //data was received in the socket and converting it into string
        var textChunk = data.toString('utf8');

        io.emit('message', textChunk);  //socket.io is supposed to send the data to the browser
        console.log(textChunk);
    });
    // Add a 'close' - "event handler" in this socket instance
    sock.on('close', function(data) {
        // closed connection
        console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
    });
}).listen(PORT, HOST);

Upvotes: 4

Views: 108

Answers (1)

Aubin
Aubin

Reputation: 14873

You may connect Java side (WebSocketServer) to Javascript side (browser) using github.com/TooTallNate/Java-WebSocket.

Java side:

final class Gateway extends WebSocketServer {

   private WebSocket _webSocket;

   Gateway( IDataManager dataManager, IConfiguration config) {
      super( new InetSocketAddress( <host>, <port> );
      new Thread( this ).start();
   }

   @Override
   public void onOpen( WebSocket conn, ClientHandshake handshake ) {
      final String request = handshake.getResourceDescriptor();
      final String[] req = request.split( "[/=]" );
      System.out.printf( "request: %s\n", Arrays.toString( req ));
      _webSocket = conn;
      ...
   }

   public void publish( ... ) {
      final ByteBuffer buffer = ByteBuffer.allocate( ... );
      buffer.order( ByteOrder.BIG_ENDIAN );
      buffer.putXXX( ... );
      buffer.flip();
      _webSocket.send( buffer );
   }

   @Override
   public void onMessage( WebSocket conn, String buffer ) {
      System.out.printf( "%s\n", buffer );
   }

   @Override
   public void onMessage( WebSocket conn, ByteBuffer buffer ) {
      try {
         System.out.printf( "%d bytes received from %s",
            buffer.remaining(), conn.getRemoteSocketAddress());
         if( buffer.position() == buffer.limit()) {
            buffer.flip();
         }
         buffer.order( ByteOrder.BIG_ENDIAN );
         final byte xxx = buffer.getXxx();
         ...
      }
      catch( final Throwable t ) {
         t.printStackTrace();
      }
   }

   @Override
   public void onError( WebSocket conn, Exception ex ) {
      ex.printStackTrace();
   }

   @Override
   public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
      System.out.printf( "code: %d, reason: %s, remote: %s\n", code, reason, remote ? "true" : "false" );
   }
}

Javascript side:

var webSocket = new WebSocket(
   'ws://'    + smoc.PROTOCOL_HOST +
   ':'        + smoc.PROTOCOL_PORT +
   '/viewID=' + $scope.viewID );
$scope.webSocket.binaryType = "arraybuffer";
$scope.webSocket.onmessage = function( evt ) {
   ...
};

Upvotes: 1

Related Questions