来自JavaScript Websocket的InputStream Java没有收到完整的消息

时间:2016-11-04 18:49:44

标签: javascript java websocket inputstream

我有以下设置: Index.html(WebPage)

    <div id="sse">
         <a href="javascript:WebSocketTest()">Run WebSocket</a>
         <a href="javascript:websocketsend()">Send message</a>
      </div>

    <script type="text/javascript">
        var ws;
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               console.log("WebSocket is supported by your Browser!");

               // Let us open a web socket
               ws = new WebSocket("ws://localhost:6790/");

               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()

                  ws.send("Hello_World!");
                  console.log("Message is sent...");

               };

               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  console.log("Message is received...");
               };

               ws.onclose = function(event)
               { 
                    var reason;
                  // websocket is closed.
                  console.log(event.code); 
                  console.log("closed.")
               }

               ws.onerror = function (evt)
               {
                    console.log(evt);
               }
            }

            else
            {
               // The browser doesn't support WebSocket
               console.log("WebSocket NOT supported by your Browser!");
            }
         }
    </script>

Java套接字服务器

private String decodeMessage(InputStream in) {
    Main.debug(in + "");
    try { 
        in.skip(2);
        ArrayList<Byte> keys = new ArrayList<Byte>();
        for(int i = 0; i < 4; i++){
            int r = in.read();
            Main.debug("added: " + r);
            keys.add((byte) r);
        }

        ArrayList<Byte> encoded = new ArrayList<Byte>();
        for(int i = 0; i < in.available(); i++){
            int r = in.read();
            Main.debug("added2: " + r);
            encoded.add((byte) r);
        }

        ArrayList<Byte> decoded = new ArrayList<Byte>();
        for(int i = 0; i < encoded.size(); i++){
            decoded.add((byte) (encoded.get(i) ^ keys.get(i & 0x3)));
            Main.debug("Decoded: " + (encoded.get(i) ^ keys.get(i & 0x3)));
        }

        Main.debug("Total: " + decoded);
        String s = new String(toByteArray(decoded), "US-ASCII");
        Main.debug("Text: " + s);

    } catch(IOException e) {

    }

    return null;
}       

public static byte[] toByteArray(List<Byte> in) {
    final int n = in.size();
    byte ret[] = new byte[n];
    for (int i = 0; i < n; i++) {
        ret[i] = in.get(i);
    }
    return ret;
}

问题是消息得到了很好的解决,但是我把它作为输出 (除了“已添加”和“已解码”调试消息,

[19:42:02 INFO]:总计:[72,101,108,108,111,95] [19:42:02 INFO]:文字:Hello_

所以“世界!”即使我用空格替换_也会丢失。 我有什么东西可以忽略吗?

我希望有人能在这里给我一个提示。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

将其转换为DataInputStream然后

while(datainputstream.available() > 0){
     ....
}

似乎要解决它! 感谢@Tinus帮助我找到这个解决方案!