nodejs没有将websocket发送到浏览器

Posted

技术标签:

【中文标题】nodejs没有将websocket发送到浏览器【英文标题】:nodejs not sending websocket to browser 【发布时间】:2018-08-11 21:08:40 【问题描述】:

我制作了一个程序来连接我的 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);

【问题讨论】:

我该怎么做? 【参考方案1】:

您可以使用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 ) 
   ...
;

【讨论】:

以上是关于nodejs没有将websocket发送到浏览器的主要内容,如果未能解决你的问题,请参考以下文章

Websockets:从NodeJS websocket服务器到带有WebSocketSharp的C#客户端的多个响应

用于大量数据的 WebSockets 与 XHR

如何使用 WebSockets 将多个文件发送到服务器?

如何将带有状态码的 WebSocket 关闭帧发送到浏览器?

nodejs-websocket介绍

通过 Websockets 在 Nodejs 和 Python 之间进行通信