The standard says that I need to send 0x00 byte, following by byte of UTF8, and end with 0xFF byte
R?e?a?l?l?y??? ?W?h?e?r?e? ?d?i?d? ?y?o?u? ?g?e?t? ?t?h?a?t??? ?I? ?d?o?n?'?t? ?t?h?i?n?k? ?t?h?e?r?e? ?i?s? ?s?o?m?e?t?h?i?n?g? ?l?i?k?e? ?t?h?i?s? ?i?n? ?t?h?e? s?p?e?c?i?f?i?c?a?t?i?o?n?.
Simonic statet in his comment that starting with 0x00 and ending with 0xFF was used in the deprecated Hixie-76 variant of the protocol.
Have a look at the actual specification (RFC 6455).
For better understanding, have a look at this great answer:
The frames you're sending need to be formatted according to the
WebSocket framing format. For sending messages, this format is as
follows:
- one byte which contains the type of data (and some additional info
which is out of scope for a trivial server)
- one byte which contains the length
- either two or eight bytes if the length does not fit in the
second byte (the second byte is then a code saying how many bytes are
used for the length)
- the actual (raw) data
I translated this java implementation for you (in a quick'n'dirty way, there may be some errors) to VB.Net, and it's working:
Sub SendMessage(sck As Socket, message as String)
Dim rawData = System.Text.Encoding.UTF8.GetBytes(message)
Dim frameCount = 0
Dim frame(10) As byte
frame(0) = cbyte(129)
if rawData.length <= 125 Then
frame(1) = CByte( rawData.length)
frameCount = 2
else if rawData.length >= 126 AndAlso rawData.length <= 65535
frame(1) = CByte( 126)
Dim len = cbyte(rawData.length)
frame(2) = CByte(((len >> 8 ) & CByte(255)))
frame(3) = CByte((len & CByte(255)))
frameCount = 4
else
frame(1) = CByte( 127)
Dim len = CByte( rawData.length)
frame(2) = CByte(((len >> 56 ) & CByte(255)))
frame(3) = CByte(((len >> 48 ) & CByte(255)))
frame(4) = CByte(((len >> 40 ) & CByte(255)))
frame(5) = CByte(((len >> 32 ) & CByte(255)))
frame(6) = CByte(((len >> 24 ) & CByte(255)))
frame(7) = CByte(((len >> 16 ) & CByte(255)))
frame(8) = CByte(((len >> 8 ) & CByte(255)))
frame(9) = CByte((len & CByte(255)))
frameCount = 10
End If
Dim bLength = frameCount + rawData.length
Console.WriteLine(frameCount)
Console.WriteLine(rawData.length)
Dim reply(bLength+1) as byte
Dim bLim = 0
for i=0 to frameCount-1
Console.WriteLine(blim)
reply(bLim) = frame(i)
bLim += 1
Next
for i=0 to rawData.length-1
Console.WriteLine(blim)
reply(bLim) = rawData(i)
bLim += 1
Next
sck.Send(reply)
End Sub
You can use it in your clientProc
:
'' Sending Hello World message
SendMessage(sck, "Hello World")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…