《websocket协议详解》教程分三篇:
文章上方有详细的规范、源码链接,你有任何问题可以联系我:邮箱:952125505@qq.com ,QQ交流群:715895604
根据前面两节讲解,我们了解了websocket是做什么,并详细的了解了websocket的协议规范,下面我们就用vb制作一个websocket控件。
首先打开vb,建立一个自定义控件,命名为WebSock
Option Explicit
'★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★ 。。
'│ 。。。。 │
'☆ 。。 。。 ☆
'│ 。。 。。 │
'★ 。。 。。 ★
'│ 。。 。。 │
'☆ 。。 。。 ☆
'│ 。。 OOO 。。 │
'★ 。。 QQQQQQQ 。。 ★
'│ 。。 88888888888 。。 │
'☆ 。。 8 。 8 。。 ☆
'│ 。。 H - - H 。。 . │
'★ 。。 0│ │0 。。 ★
'│ 。。 │ ╰-╯ │ 。。 . │
'☆ 。。 ╰-------╯ 。。 . ☆
'│ 。。 。。。 。。 . │
'★ 。。╭ ╮。。 . ★
'│ ╭ ╰ ╯ ╮ . │
'☆ ╭ ╰ ╯ ╮ . ☆
'│ ╭ ╰ ╯ ╮ . │
'★ ( ╰ v╯ ) . ★
'│ ╭ ╮ . │
'☆ ╭ ╮ . ☆
'│ ( ) . │
'★ (。)(。)(。)(。)(。)(。)(。)(。) . ★
'│ (。)(。)(。)(。)(。)(。)(。)(。)(。) . │
'☆ [0][0][0][0][0][0][0][0][0][0][0][0][0] . ☆
'│ [0][0][0][0][0][0][0][0][0][0][0][0][0] │
'★ . ★
'│ ******************************************************* │
'☆ 佛祖保佑 永无BUG ☆
'│ │
'★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★─☆─★
'****************************************************************************************
'* 模块名称: 二郎websocket完美客户端
'* 功能描述:
'****************************************************************************************
'创建:2019-09-01;作 者:二郎666 QQ:952125505;版本:Version 1.0.1(2019-09-01) . .
'连接参数
Private Type urlType
url As String
Proc As String
Host As String
Port As String
Path As String
Header As String
wsKey As String
Accept As String
End Type
'基本帧协议
Private Type abnfType
FIN As Boolean
RSV1 As Boolean
RSV2 As Boolean
RSV3 As Boolean
OPCode As Long
FrameCode As Long
MaskBool As Boolean
MaskKey() As Byte
DataLenL As Long
DataLenS As Long
DataStart As Long
DataByte() As Byte
ALen As Long
End Type
'websocket状态
Enum stateEnum
off = 0
onConnect = 1
Busy = 2
End Enum
Enum opcodeEnum
opContin = 0
opText = 1
opBinary = 2
'3 - 7 非控制帧保留
opclose = 8
opping = 9
oppong = 10
'11-15 控制帧保留
End Enum
Public Enum opMsgenum
msgContin = 0
msgtext = 1
msgBinary = 2
End Enum
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const CP_UTF8 = 65001
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
Private Declare Function send Lib "ws2_32.DLL" (ByVal socket As Long, Buf As Any, ByVal buflen As Long, ByVal Flags As Long) As Long
Private Const wsGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
Private SendUrl As urlType
Private OPCode As opcodeEnum
Private ABNF As abnfType
Dim isConnect As Boolean
'缺省属性值:
Private Const m_def_State = 0
'属性变量:
Dim m_State As stateEnum
'事件声明:
Event OnMessage(ByVal RecvData As Variant, ByVal GetMsg As Long, sFIN As Boolean)
Event OnOpen()
Event OnClose()
Event SendComplete()
Event OnError(Number As Long, Str As String)
'
'
'
'
Public Sub Connect(ByVal url As String)
m_State = onConnect
If Winsock1.State <> 0 Then
Winsock1.Close
Sleep2 10
End If
SendUrl = jiexiUrl(url)
Winsock1.Connect SendUrl.Host, SendUrl.Port
'
End Sub
Private Sub Winsock1_Connect()
isConnect = True