VBA WebSocket Communication with WinHttpWebSocketSend and WinHttpWebSocketReceive
Dim message(1) As String message(0) = '{"session_hash": "f4bqo5ku927", "fn_index": 3}' Dim cdwMessageLength As Long cdwMessageLength = 2 * Len(message(0)) ' two bytes per character
' Send and receive data on the websocket protocol. dwError = WinHttpWebSocketSend(hWebSocketHandle, _ WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE, StrPtr(message(0)), cdwMessageLength) If (dwError <> ERROR_SUCCESS) Then dwError = GetLastError GoTo quit End If Debug.Print "Sent message to the server: " & message(0) ReDim rgbBuffer(1023) As Byte Dim dwBufferLength As Long
' get correct value in case of change to buffer size dwBufferLength = (UBound(rgbBuffer) - LBound(rgbBuffer) + 1)
Dim pbCurrentBufferPointer As Long pbCurrentBufferPointer = VarPtr(rgbBuffer(0)) Dim dwBytesTransferred As Long dwBytesTransferred = 0 Dim eBufferType As Long eBufferType = 0 Dim dwCloseReasonLength As Long dwCloseReasonLength = 0 Dim usStatus As Integer usStatus = 0
' loop if we get a message fragment packet ' added a failsafe counter to original code - probably unnecessary Dim MessageComplete As Boolean, count As Long MessageComplete = False count = 0 Do Until (MessageComplete Or (count > 40))
If (dwBufferLength = 0) Then
dwError = ERROR_NOT_ENOUGH_MEMORY
GoTo quit
End If
count = count + 1
Debug.Print "Check receive buffer: " & count
' receive - will hang if no server response
dwError = WinHttpWebSocketReceive(hWebSocketHandle, rgbBuffer(0), dwBufferLength, dwBytesTransferred, eBufferType)
If (dwError <> ERROR_SUCCESS) Then
dwError = GetLastError
GoTo quit
End If
' If we receive just part of the message restart the receive operation.
pbCurrentBufferPointer = pbCurrentBufferPointer + dwBytesTransferred
dwBufferLength = dwBufferLength - dwBytesTransferred
If Not (eBufferType = WINHTTP_WEB_SOCKET_BINARY_FRAGMENT_BUFFER_TYPE) Then
MessageComplete = True
End If
' Application.Wait Now + TimeValue("00:00:05") Loop
'' ' Expected server to just echo single binary message - complain if different ''' If (eBufferType <> WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE) Then ''' Debug.Print "Unexpected buffer type" ''' dwError = 87 ' ERROR_INVALID_PARAMETER ''' GoTo quit ''' End If
' convert buffer into string (crudely, just ignore zeros) ' Dim j As Long, strBuffer As String strBuffer = "" For j = LBound(rgbBuffer) To UBound(rgbBuffer) If rgbBuffer(j) <> 0 Then strBuffer = strBuffer & Chr(rgbBuffer(j)) End If Next Debug.Print "Received message from the server: " & strBuffer
message(1) = '{"fn_index": 3, "data": ["dog wearing a hat", "", 9], "session_hash": "f4bqo5ku927"}'
cdwMessageLength = 2 * Len(message(1)) ' two bytes per character
' Send and receive data on the websocket protocol. dwError = WinHttpWebSocketSend(hWebSocketHandle, _ WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE, StrPtr(message(1)), cdwMessageLength) 为什么第二次WinHttpWebSocketSend 是12030 第一次是 零内容:The error code 12030 typically indicates a connection timeout or a dropped connection. It is possible that the connection was lost between the first and second sends, or there may be a problem with the server or network that is causing the connection to fail. It is also possible that there is an issue with the code itself, such as incorrect parameters or a memory allocation error. Further investigation or debugging may be necessary to determine the exact cause of the error.
原文地址: https://www.cveoy.top/t/topic/nr8D 著作权归作者所有。请勿转载和采集!