我尝试通过Websocket创建和应用。当我尝试向客户端客户端发送任何消息关闭连接时,我做了握手,因为WebSocket错误:网络错误12152
我猜问题是编码数据。我使用了基于Websocket new frame byte
的脚本但它不能正常工作。我希望你能帮帮我 这是我的代码:
Dim rawData = System.Text.Encoding.UTF8.GetBytes("hello")
Dim frameCount = 0
Dim frame(10) As Byte
frame(0) = CByte(129)
If rawData.Length <= 125 Then
frame(1) = CByte(rawData.Length + 1)
frameCount = 2
ElseIf rawData.Length >= 126 AndAlso rawData.Length <= 65535 Then
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) 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
For i = 0 To reply.Length - 1
'Console.WriteLine("Byte: " & reply(i))
'Console.WriteLine("Char: " & CByte(reply(i)))
Next
Dim asd As String = Encoding.UTF8.GetString(reply)
Console.WriteLine(asd)
pv_streamwriter.WriteLine(asd)
pv_streamwriter.Flush()
答案 0 :(得分:0)
感谢您使用我的代码。绝对酷看。
无论如何,我自己发现了发送功能这个烦人的问题。
根据RFC6455您必需在向服务器发送消息时屏蔽已发送的消息,但在将消息从服务器发送到客户端时不需要屏蔽消息。现在,我已经说过,我已经改变了这个功能,包括从客户端到服务器的屏蔽。我将删除我的特定掩码,因为我不想在网上。希望这会有所帮助。
关于创建安全掩码的说明。 RFC6455提到了RFC4086:
The masking key is a 32-bit value chosen at random by the client.
When preparing a masked frame, the client MUST pick a fresh masking
key from the set of allowed 32-bit values. The masking key needs to
be unpredictable; thus, the masking key MUST be derived from a strong
source of entropy, and the masking key for a given frame MUST NOT
make it simple for a server/proxy to predict the masking key for a
subsequent frame. The unpredictability of the masking key is
essential to prevent authors of malicious applications from selecting
the bytes that appear on the wire. [RFC 4086][RFC4086] discusses what
entails a suitable source of entropy for security-sensitive
applications.
老实说,我只是想在我遵循该文档之前让它工作,所以我使用了Rnd()
,模数和其他一些东西的组合来使其难以解码。
Sub SendMessage2(ByVal socket As Socket, ByVal message As String, ByVal len As Integer)
Dim frameCount As Integer
Dim len16 As UInt16
Dim reply(len + 8) As [Byte]
Dim frame(10) As [Byte]
Dim maskingBytes(4) As [Byte]
frame(0) = CByte(&H81)
If (len <= 125) Then
frame(1) = CByte(len + 128)
frameCount = 2
ElseIf (len >= 126 AndAlso len <= 65535) Then
frame(1) = CByte(126 + 128)
len16 = Convert.ToUInt16(len)
frame(2) = CByte(BitConverter.GetBytes(len16).GetValue(0))
frame(3) = CByte(BitConverter.GetBytes(len16).GetValue(1))
frameCount = 4
Else
frame(1) = CByte(127 + 128)
'Odds are we are not ever going to get here. but here is some code
'frame(2) = CByte(((len >> 56) And CByte(255)))
'frame(3) = CByte(((len >> 48) And CByte(255)))
'frame(4) = CByte(((len >> 40) And CByte(255)))
'frame(5) = CByte(((len >> 32) And CByte(255)))
'frame(6) = CByte(((len >> 24) And CByte(255)))
'frame(7) = CByte(((len >> 16) And CByte(255)))
'frame(8) = CByte(((len >> 8) And CByte(255)))
'frame(9) = CByte((len) And CByte(255))
frameCount = 10
End If
For i As Integer = 0 To frameCount - 1
reply(i) = Convert.ToByte(frame(i))
Next
For i As Integer = 0 To 3
Randomize()
maskingBytes(i) = 0'This is where you need to create your own masking byte
reply(frameCount + i) = maskingBytes(i)
Next
For i As Integer = 0 To len
If (Not i = len) Then
reply(frameCount + i + 4) = Convert.ToByte(message.Chars(i)) Xor maskingBytes(i Mod 4)
Else
reply(frameCount + i + 4) = CByte(0) Xor maskingBytes(i Mod 4)
End If
Next
If (socket.Send(reply) <= 0) Then
Console.WriteLine("WE ARE NOT WRITING!!")
End If
End Sub