VBA WinHttpWebSocketSend Error 12030: Connection Terminated or Reset

The error code 12030 in VBA's WinHttpWebSocketSend function signifies that the connection with the server was terminated or reset during the request. This can arise due to various reasons, including network issues, server overload, or server configuration problems.

Understanding the Issue

The error typically occurs when a request is sent via WinHttpWebSocketSend but the connection is abruptly broken before the server can process it. This can leave the request in a limbo state, resulting in the error code 12030.

Possible Solutions

Here are some common solutions to troubleshoot the error:

  1. Network Connectivity Check: Ensure your network connection is stable and reliable. Intermittent network issues can lead to connection disruptions. Examine the network environment for potential disruptions like high latency or packet loss.

  2. Increase Timeouts: Increase the timeout values for WinHttp requests to provide the server with more time to respond. This is particularly helpful if the server is under heavy load.

  3. Review Server Logs: Inspect the server logs for any errors or warnings that might indicate the cause of the connection termination. These logs often provide valuable insights into the server-side issues.

  4. Adjust Request Size/Frequency: Experiment with sending smaller messages or reducing the frequency of requests. This can help identify if server overload is contributing to the issue.

Example Code

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)
If (dwError <> ERROR_SUCCESS) Then
    dwError = GetLastError
    GoTo quit
End If
Debug.Print "Sent message to the server: " & message(1)

' ... (rest of your code)

quit:
' ... (handle error)
End If

Important Note: This information aims to provide general guidance on resolving the error code 12030. The specific solution will depend on the particular circumstances of your application and server environment. If the issue persists, consider reaching out to the server administrators or seeking further assistance from the relevant technical communities.

VBA WinHttpWebSocketSend Error 12030: Connection Terminated or Reset

原文地址: https://www.cveoy.top/t/topic/nr8I 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录