簡體   English   中英

node.js沒有將websocket發送到瀏覽器

[英]nodejs not sending websocket to browser

我制作了一個程序連接我的java程序,該程序使用套接字將數據發送到我的nodejs服務器,而nodejs服務器應該使用socket.io將接收到的數據發送到瀏覽器,但是有一個問題,我確實從Java接收了數據,但是節點服務器沒有將其發送到瀏覽器,這里是代碼

// 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);

您可以使用github.com/TooTallNate/Java-WebSocket將Java端(WebSocketServer)連接到Javascript端(瀏覽器)。

Java方面:

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方面:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM