How to parse and validate a WebSocket frame in Java?

前端 未结 4 2010
慢半拍i
慢半拍i 2021-01-14 19:10

I wrote a WebSocket frame decoder in Java:

private byte[] decodeFrame(byte[] _rawIn) {
        int maskIndex = 2;
        byte[] maskBytes = new byte[4];

           


        
4条回答
  •  生来不讨喜
    2021-01-14 19:35

    My current interest in websockets allows me to possibly help with this though I'm brand new to websockets.

    http://tools.ietf.org/html/rfc6455#section-5.2 gives a high level view of the data frame. You will test the last four of the first byte so raw_in[0]<<<4. This will give you the last four I'm not too good with bit operations yet so I'm not sure how to get the last 4 bits to represent 0000 1111-0000 0000 vs 1111 0000-0000 0000. So as you can see 0001 op code is a text frame, 0010 op code is a binary frame and so on. So if you only want to except text frames simply test that the last four bits of the first byte is 0001.

提交回复
热议问题