我目前正在编写一个Visual Basic脚本,在本地计算机ip(127.0.0.1)和端口8181上打开TcpListener。我从Google Chrome连接到我的websocket服务器(通过AcceptSocket方法)。从Chrome我发送一个发送请求到服务器,服务器可以读取它和一切。当我发回消息(如回声)时,我可以第一次发送它,但第二次发出错误:
Received unexpected continuation frame.
我需要的是一个CByte,告诉框架开始一个新的或结束一个开放的(不关闭套接字连接)
这是否可行,如果可以,解决方案是什么?
我用SendMessage(sck, "test")
以下是我的邮件发件人的代码:
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
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 + 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
For i = 0 To reply.Length - 1
Console.WriteLine("Byte: " & reply(i))
Console.WriteLine("Char: " & CByte(reply(i)))
Next
sck.Send(reply, reply.Length, 0)
End Sub
更新
将行Dim reply(bLength + 1) As Byte
更改为Dim reply(bLength) As Byte
后,我不会收到连续帧错误,而是会收到新错误:
A server must not mask any frames that it sends to the client.
希望这些信息可以指出问题。
更新2:
以下是整个客户端Sub对客户端做的一切。从接受到握手到消息。
Private Sub clientStarter(ByVal sck As Socket)
Dim netStream As New NetworkStream(sck)
Dim netReader As New IO.StreamReader(netStream)
Dim netWriter As New IO.StreamWriter(netStream)
Dim key As String = ""
Console.WriteLine("Accept new connection ...")
'' Reading handshake message
While (True)
Dim line As String = netReader.ReadLine()
If line.Length = 0 Then
Exit While
End If
If (line.StartsWith("Sec-WebSocket-Key: ")) Then
key = line.Split(":")(1).Trim()
End If
Console.WriteLine("Data: " & line)
End While
'' Calculate accept-key
key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
key = getSHA1Hash(key)
'' Response handshake message
Dim response As String
response = "HTTP/1.1 101 Switching Protocols" & vbCrLf
response &= "Upgrade: websocket" & vbCrLf
response &= "Connection: Upgrade" & vbCrLf
response &= "Sec-WebSocket-Accept: " & key & vbCrLf & vbCrLf
netWriter.Write(response)
netWriter.Flush()
Dim dataSent As [Byte]() = Nothing
Dim dataRecieved(1024) As [Byte]
Dim bytes As Int32
Console.WriteLine(dataRecieved)
Dim message As String = Nothing
While (True)
If (sck.Connected = False) Then
Exit While
End If
bytes = sck.Receive(dataRecieved, dataRecieved.Length, 0)
'Console.WriteLine(bytes)
While (bytes > 0)
Console.WriteLine("recieved Data")
message = System.Text.Encoding.UTF8.GetString(dataRecieved, 0, bytes)
Console.WriteLine(bytes)
bytes = 0
If (bytes = 0) Then
Console.WriteLine("test")
Console.WriteLine(message)
dataSent = System.Text.Encoding.UTF8.GetBytes(message, 0, bytes)
SendMessage(sck, "test")
message = Nothing
End If
End While
End While
End Sub
更新3(解决方案):
我已经弄清楚了我的问题。通过将frame(1) = CByte(rawData.Length)
更改为frame(1) = CByte(rawData.Length + 1)
,它会将结束0字节包含在内,现在消息的正确长度是正确的,并且让我知道我的屏蔽问题。我想消息字节的结尾应该包含在消息长度中。
答案 0 :(得分:1)
看起来你在邮件末尾加了一个额外的字节。不应该行
Dim reply(bLength + 1) As Byte
改为
Dim reply(bLength) As Byte
代替?您在消息中需要的只是操作码+长度(frameCount
)+消息(rawData.Length
)。 websocket客户端将这些字节读作完整有效的消息。然后,它将从您的消息中读取剩余的字节作为新消息的开头,从而有效地破坏您实际发送的下一条消息。