5月12022
autoit WebSocket示例WS协议
Autoit V3 WebSocket示例 WS协议
;http://code.msdn.microsoft.com/windowsdesktop/WinHTTP-WebSocket-sample-50a140b5/sourcecode?fileId=51199&pathId=1032775223
#include "WinHttp_WebSocket.au3"
#include <WinAPI.au3> ;_WinAPI_GetLastError
#include <APIErrorsConstants.au3>
Global $hOpen = 0, $hConnect = 0, $hRequest = 0, $hWebSocket = 0
Global $iError = 0
Example()
Exit quit()
Func Example()
Local $wsip[]
$wsip['ip']= "121.40.165.18"
$wsip['端口'] = 8800
$wsip['目录'] = ''
$wsip['发送内容'] = '你好'
ConsoleWrite("====================" & @crlf)
; 创建会话、连接和请求句柄。
;-----------------------------------------------------------------------------------------拿到WS握手包
$hOpen = _WinHttpOpen("WebSocket 示例", $WINHTTP_ACCESS_TYPE_DEFAULT_PROXY)
If $hOpen = 0 Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("Http打开失败" & @CRLF)
Return False
EndIf
$hConnect = _WinHttpConnect($hOpen, $wsip['ip'], $wsip['端口'])
If $hConnect = 0 Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("Http连接失败" & @CRLF)
Return False
EndIf
$hRequest = _WinHttpOpenRequest($hConnect, "GET", $wsip['目录'], "")
If $hRequest = 0 Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("打开请求失败" & @CRLF)
Return False
EndIf
; 请求ws链接.从http转到 WS
Local $fStatus = _WinHttpSetOptionNoParams($hRequest, $WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET)
If Not $fStatus Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("请求转入失败" & @CRLF)
Return False
EndIf
; 通过发送http请求并接收服务器的响应来执行websocket握手。 如有必要需要指定头部参数
$fStatus = _WinHttpSendRequest($hRequest)
If Not $fStatus Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("发送请求失败" & @CRLF)
Return False
EndIf
$fStatus = _WinHttpReceiveResponse($hRequest)
If Not $fStatus Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("接收响应失败" & @CRLF)
Return False
EndIf
; 检查服务器返回的HTTP状态码是什么,并相应地进行操作。
; 如果HTTP状态代码不同于101,WinHttpWebSocketCompleteUpgrade将失败。
$iExtended = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_STATUS_CODE)
If $iExtended <> 101 Then
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iExtended = ' & $iExtended & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Return False
EndIf
$hWebSocket = _WinHttpWebSocketCompleteUpgrade($hRequest, 0)
If $hWebSocket = 0 Then
$iError = _WinAPI_GetLastError()
ConsoleWrite("转入WS失败" & @CRLF)
Return False
EndIf
_WinHttpCloseHandle($hRequest)
;----------------------------------------------------------------------------拿WS握手结束
$hRequestHandle = 0
ConsoleWrite("成功转入至WS协议" & @CRLF)
; ; 通过websocket协议发送和接收数据
;-----------------------------------------------------------------------------发消息
$iError = _WinHttpWebSocketSend($hWebSocket,$WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE,$wsip['发送内容'])
If @error Or $iError <> 0 Then
ConsoleWrite("WS发送失败" & @CRLF)
Return False
EndIf
ConsoleWrite("消息成功发送到服务器: " & $wsip['发送内容'] & @CRLF)
;-----------------------------------------------------------------------------收消息
Local $iBufferLen = 1024
Local $tBuffer = 0, $bRecv = Binary("")
Local $iBytesRead = 0, $iBufferType = 0
Do ;----------循环接收消息
If $iBufferLen = 0 Then
$iError = $ERROR_NOT_ENOUGH_MEMORY
Return False
EndIf
$tBuffer = DllStructCreate("byte[" & $iBufferLen & "]")
$iError = _WinHttpWebSocketReceive($hWebSocket,$tBuffer,$iBytesRead,$iBufferType)
If @error Or $iError <> 0 Then
ConsoleWrite("WS接收错误" & @CRLF)
Return False
EndIf
$bRecv &= BinaryMid(DllStructGetData($tBuffer, 1), 1, $iBytesRead)
$tBuffer = 0
$iBufferLen -= $iBytesRead
Until $iBufferType <> $WINHTTP_WEB_SOCKET_UTF8_FRAGMENT_BUFFER_TYPE
;我们希望服务器只回显单个二进制消息。
If $iBufferType <> $WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE Then
ConsoleWrite("Unexpected buffer type" & @CRLF)
$iError = $ERROR_INVALID_PARAMETER
Return False
EndIf
;-------------------------------- -------收到的消息
ConsoleWrite("收到来自服务器的消息: '" & BinaryToString($bRecv,4) & "'" & @CRLF)
; 关闭WS链接.--------------------------------------------------------------------
$iError = _WinHttpWebSocketClose($hWebSocket, _
$WINHTTP_WEB_SOCKET_SUCCESS_CLOSE_STATUS)
If @error Or $iError <> 0 Then
ConsoleWrite("WS关闭错误" & @CRLF)
Return False
EndIf
;检查服务器返回的关闭状态。
Local $iStatus = 0, $iReasonLengthConsumed = 0
Local $tCloseReasonBuffer = DllStructCreate("byte[123]")
$iError = _WinHttpWebSocketQueryCloseStatus($hWebSocket,$iStatus,$iReasonLengthConsumed,$tCloseReasonBuffer)
If @error Or $iError <> 0 Then
ConsoleWrite("查看错误关闭状态" & @CRLF)
Return False
EndIf
ConsoleWrite("关闭链接状态码: '" & $iStatus & "' 和原因: '" & BinaryToString(BinaryMid(DllStructGetData($tCloseReasonBuffer, 1), 1, $iReasonLengthConsumed)) & "'" & @CRLF)
EndFunc ;==>Example
;关闭http会话
Func quit()
If $hRequest <> 0 Then
_WinHttpCloseHandle($hRequest)
$hRequest = 0
EndIf
If $hWebSocket <> 0 Then
_WinHttpCloseHandle($hWebSocket)
$hWebSocket = 0
EndIf
If $hConnect <> 0 Then
_WinHttpCloseHandle($hConnect)
$hConnect = 0
EndIf
If $iError <> 0 Then
ConsoleWrite("应用程序失败出现错误: " & $iError & @CRLF)
Return -1
EndIf
Return 0
EndFunc
下载全部:
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!