Rss & SiteMap
Foxtable(狐表) http://www.foxtable.com
|
Public wssv As New wss.WebSocketServer() 'WebSocket容器Public wssh As Object '将来挂载进去WebSocket容器的实例'Websocket的实例代码Public Class WsChatInherits wss.WebSocketBehavior'默认构造函数Public Sub New()End Sub'1重写客户端上线后的事件Protected Overrides Sub OnOpen()Functions.Execute("WsUserLogged",Id,Context.Origin,Context.QueryString)End Sub'2重写收到客户端下线的事件Protected Overrides Sub OnClose(ByVal e As ws.CloseEventArgs)Functions.Execute("WsUserLogout",Id)End Sub'3重写收到客户端消息后的事件Protected Overrides Sub OnMessage(ByVal e As ws.MessageEventArgs)Functions.Execute("WsReceivedMessage",Id,e.Data)End Sub'4重写报错异常事件Protected Overrides Sub OnError(ByVal e As ws.ErrorEventArgs)Functions.Execute("WsOnError",e.Message)End SubEnd Class
函数名:WsKickOff
'踢某个ID的用户下线'小技巧 wssh.Sessions.Ids是所有在线用户的ID集合,可以用来Count或者判断Contains'通过Functions.Execute("WsKickOff",ID)使用Dim ID As String =Args(0)If wssh.Sessions.Ids.Contains(ID) Thenwssh.Sessions.CloseSession(ID)End If
函数名:WsSendAll
'群发消息'通过Functions.Execute("WsSendAll","xxx")使用Dim s1 As String = Args(0)wssv.WebSocketServices.Broadcast(s1)
函数名:WsSendOne
'对某个ID的用户发送文本消息。Ele的type类型有4种:success,warning,info,error'小技巧 wssh.Sessions.Ids是所有在线用户的ID集合,可以用来Count或者判断Contains'通过Functions.Execute("WsSendOne",ID,"xxx")使用Dim ID As String = Args(0)Dim s1 As String = Args(1)If wssh.Sessions.Ids.Contains(ID) Thenwssh.Sessions.SendTo(s1,ID)End If
函数名:WsOnError
函数名:WsReceivedMessage'当WebSocket服务器端异常报错的事件Dim msg As String =Args(0)'收到WebSocket客户端消息后的事件Dim ID As String =Args(0)Dim msg As String =Args(1)
函数名:WsUserLogged
'WebSocket客户端上线后的事件,知道登陆用户的ID,和其他数据Dim ID As String =Args(0)Dim Origin As String = Args(1) '客户端请求地址Dim cqs As System.Collections.Specialized.NameValueCollection = Args(2) '通过 cqs("token")来获取value值,注意区分大小写,注意是否为Nothing
函数名:WsUserLogout
5更新代码精灵'WebSocket客户端下线后的事件,知道下线用户的IDDim ID As String =Args(0)
wssv = New wss.WebSocketServer("ws://127.0.0.1:9091")wssv.AddWebSocketService(Of WsChat)("/WsChat")'尝试启动Trywssv.Start()'把ws实例放进去全局变量,方便使用Dim lwh As List(of wss.WebSocketServiceHost) = wssv.WebSocketServices.HostsFor Each wh As wss.WebSocketServiceHost In lwhIf wh.Path="/WsChat" Thenwssh = whExit ForEnd IfNextCatch ex As ExceptionMessagebox.Show("Websocket启动失败:" & ex.Message)End Try
If wssv.IsListening Thenwssv.Stop()End If
let ws = null//方法://主动创建ws连接function wsConnect() {if (typeof(WebSocket) === "undefined") {console.log('您的浏览器不支持WebSocket')} else {if (!ws || ws.readyState == 0 || ws.readyState == 3) {//根据开发环境配置path,注意是否支持https,决定这里是ws还是wss,否则会被nginx拦截let path = 'ws://127.0.0.1:9091/WsChat'// 实例化socketws = new WebSocket(path)// 重写socket连接的方法ws.onopen = wsOnOpen// 重写socket监听错误消息的方法ws.onerror = wsOnError// 重写socket接收消息的方法ws.onmessage = wsOnMessage// 重写socket关闭的方法ws.onclose = wsOnClose}}}//主动向服务器发送文本function wsSend(msg) {ws.send(msg)}//主动关闭连接function wsClose() {if (ws) {ws.close()console.log("ws主动关闭连接")}}//事件://连接成功后触发的事件function wsOnOpen() {console.log("ws连接成功后")}//连接接收信息后触发的事件function wsOnMessage(msg) {console.log('收到消息:' + msg.data)}//连接关闭后触发的事件function wsOnClose() {console.log("ws关闭连接后")}//WS报错后触发的事件function wsOnError() {console.log("ws连接错误")}
如果你用狐表做服务器直接对外,那就必须2个端口了我更加推荐使用Nginx反向代理,可以保护狐表,可以一键增加ssl安全证书(变成https和wss),可以反向代理实现对外1个端口。