aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/websockets_api/websocket_server_vb.net/index.html
blob: 3969f9c5ea3751463ee09fc930e512ab84ed0967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
---
title: WebSocket Server Vb.NET
slug: Web/API/WebSockets_API/WebSocket_Server_Vb.NET
translation_of: Web/API/WebSockets_API/WebSocket_Server_Vb.NET
---
<p>{{gecko_minversion_header("2")}}{{draft}}</p>

<p>下面的示例没有优化。没有使用 .NET 4.5 Websocket。<br>
 <br>
 当前版本:</p>

<ul>
 <li>包含了一个System.Net.Sockets.TcpClient类的泛型集合</li>
 <li>特性 - 自定义事件和委托处理程序</li>
 <li>特性 -  线程化和实现Timers.Timer</li>
 <li>演示如何使用网络流将帧写回客户机(opCode 0001)</li>
 <li>是否打算作为本教程和其他贡献者的起点</li>
</ul>

<p> </p>

<pre>Imports System.Net.Sockets
Imports System.Net
Imports System
Imports System.Text
Imports System.Text.RegularExpressions


Namespace TypeDef.WebSocket

    Public Class Client
        Dim _TcpClient As System.Net.Sockets.TcpClient

        Public Delegate Sub OnClientDisconnectDelegateHandler()
        Public Event onClientDisconnect As OnClientDisconnectDelegateHandler


        Sub New(ByVal tcpClient As System.Net.Sockets.TcpClient)
            Me._TcpClient = tcpClient
        End Sub


        Function isConnected() As Boolean
            Return Me._TcpClient.Connected
        End Function


        Sub HandShake()
            Dim stream As NetworkStream = Me._TcpClient.GetStream()
            Dim bytes As Byte()
            Dim data As String

            While Me._TcpClient.Connected
                While (stream.DataAvailable)
                    ReDim bytes(Me._TcpClient.Client.Available)
                    stream.Read(bytes, 0, bytes.Length)
                    data = System.Text.Encoding.UTF8.GetString(bytes)

                    If (New System.Text.RegularExpressions.Regex("^GET").IsMatch(data)) Then

                        Dim response As Byte() = System.Text.Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" &amp; Environment.NewLine &amp; "Connection: Upgrade" &amp; Environment.NewLine &amp; "Upgrade: websocket" &amp; Environment.NewLine &amp; "Sec-WebSocket-Accept: " &amp; Convert.ToBase64String(System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(New Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups(1).Value.Trim() &amp; "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))) &amp; Environment.NewLine &amp; Environment.NewLine)

                        stream.Write(response, 0, response.Length)
                        Exit Sub
                    Else
                        'We're going to disconnect the client here, because he's not handshacking properly (or at least to the scope of this code sample)
                        Me._TcpClient.Close() 'The next While Me._TcpClient.Connected Loop Check should fail.. and raise the onClientDisconnect Event Thereafter
                    End If
                End While
            End While
            RaiseEvent onClientDisconnect()
        End Sub


        Sub CheckForDataAvailability()
            If (Me._TcpClient.GetStream().DataAvailable) Then
                Dim stream As NetworkStream = Me._TcpClient.GetStream()
                Dim frameCount = 2
                Dim bytes As Byte()
                Dim data As String
                ReDim bytes(Me._TcpClient.Client.Available)
                stream.Read(bytes, 0, bytes.Length) 'Read the stream, don't close it..

                Try
                    Dim length As UInteger = bytes(1) - 128 'this should obviously be a byte (unsigned 8bit value)

                    If length &gt; -1 Then
                        If length = 126 Then
                            length = 4
                        ElseIf length = 127 Then
                            length = 10
                        End If
                    End If

                    'the following is very inefficient and likely unnecessary..
                    'the main purpose is to just get the lower 4 bits of byte(0) - which is the OPCODE

                    Dim value As Integer = bytes(0)
                    Dim bitArray As BitArray = New BitArray(8)

                    For c As Integer = 0 To 7 Step 1
                        If value - (2 ^ (7 - c)) &gt;= 0 Then
                            bitArray.Item(c) = True
                            value -= (2 ^ (7 - c))
                        Else
                            bitArray.Item(c) = False
                        End If
                    Next


                    Dim FRRR_OPCODE As String = ""

                    For Each bit As Boolean In bitArray
                        If bit Then
                            FRRR_OPCODE &amp;= "1"
                        Else
                            FRRR_OPCODE &amp;= "0"
                        End If
                    Next


                    Dim FIN As Integer = FRRR_OPCODE.Substring(0, 1)
                    Dim RSV1 As Integer = FRRR_OPCODE.Substring(1, 1)
                    Dim RSV2 As Integer = FRRR_OPCODE.Substring(2, 1)
                    Dim RSV3 As Integer = FRRR_OPCODE.Substring(3, 1)
                    Dim opCode As Integer = Convert.ToInt32(FRRR_OPCODE.Substring(4, 4), 2)



                    Dim decoded(bytes.Length - (frameCount + 4)) As Byte
                    Dim key As Byte() = {bytes(frameCount), bytes(frameCount+1), bytes(frameCount+2), bytes(frameCount+3)}

                    Dim j As Integer = 0
                    For i As Integer = (frameCount + 4) To (bytes.Length - 2) Step 1
                        decoded(j) = Convert.ToByte((bytes(i) Xor masks(j Mod 4)))
                        j += 1
                    Next



                    Select Case opCode
                        Case Is = 1
                            'Text Data Sent From Client

                            data = System.Text.Encoding.UTF8.GetString(decoded)
                            'handle this data

                            Dim Payload As Byte() = System.Text.Encoding.UTF8.GetBytes("Text Recieved")
                            Dim FRRROPCODE As Byte() = Convert.ToByte("10000001", 2) 'FIN is set, and OPCODE is 1 or Text
                            Dim header as byte() = {FRRROPCODE, Convert.ToByte(Payload.Length)}


                            Dim ResponseData As Byte()
                            ReDim ResponseData((header.length + Payload.Length) - 1)
                            'NOTEWORTHY: if you Redim ResponseData(header.length + Payload.Length).. you'll add a 0 value byte at the end of the response data..
                            'which tells the client that your next stream write will be a continuation frame..

                            Dim index as integer = 0

                            Buffer.BlockCopy(header, 0, ResponseData, index, header.length)
                            index += header.length

                            Buffer.BlockCopy(payload, 0, ResponseData, index, payload.length)
                            index += payload.length
                            stream.Write(ResponseData, 0, ResponseData.Length)
                      Case Is = 2
                            '// Binary Data Sent From Client
                            data = System.Text.Encoding.UTF8.GetString(decoded)
                            Dim response As Byte() = System.Text.Encoding.UTF8.GetBytes("Binary Recieved")
                             stream.Write(response, 0, response.Length)
                      Case Is = 9 '// Ping Sent From Client
                      Case Is = 10 '// Pong Sent From Client
                      Case Else '// Improper opCode.. disconnect the client
                            _TcpClient.Close()
                            RaiseEvent onClientDisconnect()
                      End Select
                Catch ex As Exception
                    _TcpClient.Close()
                    RaiseEvent onClientDisconnect()
                End Try
            End If
        End Sub
    End Class



    Public Class Server
        Inherits System.Net.Sockets.TcpListener

        Delegate Sub OnClientConnectDelegate(ByVal sender As Object, ByRef Client As WebSocket.Client)
        Event OnClientConnect As OnClientConnectDelegate


        Dim WithEvents PendingCheckTimer As Timers.Timer = New Timers.Timer(500)
        Dim WithEvents ClientDataAvailableTimer As Timers.Timer = New Timers.Timer(50)
        Property ClientCollection As List(Of WebSocket.Client) = New List(Of WebSocket.Client)



        Sub New(ByVal url As String, ByVal port As Integer)
            MyBase.New(IPAddress.Parse(url), port)
        End Sub


        Sub startServer()
            Me.Start()
            PendingCheckTimer.Start()
        End Sub



        Sub Client_Connected(ByVal sender As Object, ByRef client As WebSocket.Client) Handles Me.OnClientConnect
            Me.ClientCollection.Add(client)
            AddHandler client.onClientDisconnect, AddressOf Client_Disconnected
            client.HandShake()
            ClientDataAvailableTimer.Start()
        End Sub


        Sub Client_Disconnected()

        End Sub


        Function isClientDisconnected(ByVal client As WebSocket.Client) As Boolean
            isClientDisconnected = False
            If Not client.isConnected Then
                Return True
            End If
        End Function


        Function isClientConnected(ByVal client As WebSocket.Client) As Boolean
            isClientConnected = False
            If client.isConnected Then
                Return True
            End If
        End Function


        Private Sub PendingCheckTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles PendingCheckTimer.Elapsed
            If Pending() Then
                RaiseEvent OnClientConnect(Me, New CORE.TypeDef.WebSocket.Client(Me.AcceptTcpClient()))
            End If
        End Sub


        Private Sub ClientDataAvailableTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles ClientDataAvailableTimer.Elapsed
            Me.ClientCollection.RemoveAll(AddressOf isClientDisconnected)
            If Me.ClientCollection.Count &lt; 1 Then ClientDataAvailableTimer.Stop()

            For Each Client As WebSocket.Client In Me.ClientCollection
                Client.CheckForDataAvailability()
            Next
        End Sub
    End Class
End Namespace

Sub Main()  'Program Entry point
    Dim thread As System.Threading.Thread = New System.Threading.Thread(AddressOf StartWebSocketServer)
    'Application.Add("WebSocketServerThread", thread) 'Global.asax - context.Application .. I left this part in for web application developers
    thread.Start()
End Sub

Public Shared WebSocketServer As TypeDef.WebSocket.Server
Public Shared Sub StartWebSocketServer()
    WebSocketServer = New TypeDef.WebSocket.Server("127.0.0.1", 8000)
    WebSocketServer.startServer()
End Sub
</pre>