用vb编写websocket客户端示例(每秒百万弹幕吞吐量)

《websocket协议详解》教程分三篇:

  1.  什么是websocket
  2. websocket协议规范
  3.  用vb编写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
    
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值