VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net的ashx来作的WebSocketRequest 请求聊天代码

vb.net的ashx来作的WebSocketRequest 请求聊天代码,现在网上关于vb.net的代码越来越少了,而对于websocket的代码更少。

首页要定义一个公共用户组,然后将每一个请求的全部放到数组中挂起,用户有消息了再进行转发。

  Public ChatList As New Dictionary(Of String, Net.WebSockets.WebSocket) '当前在线的用户  
 


  1.  
    <%@ WebHandler Language="VB" Class="Chat" %>
  2.  
    Imports Newtonsoft.Json
  3.  
    Imports System.Text.Encoding '引用文本编码
  4.  
    Imports System.Threading
  5.  
    Imports System.Net.WebSockets
  6.  
    Public Class Chat : Implements IHttpHandler
  7.  
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
  8.  
    Get
  9.  
    Return False
  10.  
    End Get
  11.  
    End Property
  12.  
    Public Sub ProcessRequest(ByVal Context As HttpContext) Implements IHttpHandler.ProcessRequest
  13.  
    If Context.IsWebSocketRequest = False Then Context.Response.Write("请求类型错误") : Context.Response.End()
  14.  
    Context.AcceptWebSocketRequest(AddressOf SocketHandler) '接到一个请求
  15.  
    End Sub
  16.  
     
  17.  
    Private Async Function SocketHandler(Context As WebSockets.AspNetWebSocketContext) As Tasks.Task
  18.  
    Try
  19.  
    Dim WS As WebSocket = Context.WebSocket, ID As String = GetRequest("ID", 8)
  20.  
    If ChatList.ContainsKey(ID) = True Then '检查是否在线包含
  21.  
    ChatList(ID) = WS
  22.  
    Else '新会员
  23.  
    ChatList.Add(ID, WS) '上线
  24.  
    End If
  25.  
     
  26.  
    Dim Buffer As New ArraySegment(Of Byte)(New Byte(2048) {}), Result As WebSocketReceiveResult
  27.  
    While True '一直扫描状态
  28.  
    Select Case WS.State'连接状态
  29.  
    Case WebSocketState.None, WebSocketState.Connecting '正在连接不处理
  30.  
    Case WebSocketState.Open '已连接
  31.  
    Result = Await WS.ReceiveAsync(Buffer, CancellationToken.None)
  32.  
    If Result.EndOfMessage = True AndAlso Result.Count > 0 Then
  33.  
    Message(ID, Buffer.Array, Result.Count)
  34.  
    End If
  35.  
    Case WebSocketState.CloseSent, WebSocketState.CloseReceived, WebSocketState.Closed, WebSocketState.Aborted '断开连接
  36.  
    ChatList.Remove(ID) '下线
  37.  
    Exit While
  38.  
    End Select
  39.  
    Thread.Sleep(1000) '延迟1秒
  40.  
    End While
  41.  
    Catch ex As Exception
  42.  
    SoftLog(ex.ToString, "Chat.txt") '记录系统错误
  43.  
    End Try
  44.  
    End Function
  45.  
    '处理接受到的消息内容
  46.  
    Sub Message(UserID As String, Buffer() As Byte, Count As Integer)
  47.  
    Try
  48.  
    Dim RStr As String = Encoding.UTF8.GetString(Buffer, 0, Count), Json As Object = JsonConvert.DeserializeObject(RStr)
  49.  
    If Json("MsgType") = False Then Return
  50.  
     
  51.  
     
  52.  
     
  53.  
    If UserID <> "1" AndAlso ChatList.ContainsKey("1") = True Then
  54.  
    RStr = "成功:" & ChatList.Count
  55.  
    ChatList("1").SendAsync(New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(RStr)), WebSocketMessageType.Text, True, CancellationToken.None)
  56.  
     
  57.  
     
  58.  
     
  59.  
     
  60.  
     
  61.  
    ElseIf UserID <> "2" AndAlso ChatList.ContainsKey("2") = True Then
  62.  
    RStr = "成功:" & ChatList.Count
  63.  
    ChatList("2").SendAsync(New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(RStr)), WebSocketMessageType.Text, True, CancellationToken.None)
  64.  
    ElseIf UserID <> "3" AndAlso ChatList.ContainsKey("3") = True Then
  65.  
    RStr = "成功:" & ChatList.Count
  66.  
    ChatList("3").SendAsync(New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(RStr)), WebSocketMessageType.Text, True, CancellationToken.None)
  67.  
     
  68.  
    Else
  69.  
     
  70.  
    RStr = "当前在线:" & ChatList.Count
  71.  
    ChatList(UserID).SendAsync(New ArraySegment(Of Byte)(Encoding.UTF8.GetBytes(RStr)), WebSocketMessageType.Text, True, CancellationToken.None)
  72.  
     
  73.  
    End If '在线会员
  74.  
     
  75.  
     
  76.  
     
  77.  
     
  78.  
    Catch ex As Exception
  79.  
    ChatList(UserID).Abort() '断开连接
  80.  
    ChatList.Remove(UserID) '用户下线
  81.  
    SoftLog(ex.ToString) '记录系统错误
  82.  
    End Try
  83.  
    End Sub
  84.  
    End Class
 

这是我作的效果,包括文字、语音、视频、红包类聊天


相关教程