注意: 我将此标记为c#,因为我相信它将在ASP中吸引更广泛的受众。网络社区。如果这是一个问题,请让我知道,我可以删除它(c#是我的首选语言,但对于这个项目,我被迫用VB编写)。websocket服务器是用VB编写的。净ASP。净之前。
我只是想让连接打开,保持打开状态,并从客户端向服务器发送一个快速字符串消息。我已经减少了我的服务器代码,以尽可能简单,并张贴在下面。
以下是我的服务器代码:注意:我将注释从VB的"'"更改为c#的"//",因为它在堆栈溢出的代码块中显示为字符串。
Public Class ChatController
Inherits ApiController
//This is getting hit no problem
Public Function [Get](username As String) As HttpResponseMessage
If HttpContext.Current.IsWebSocketRequest Then
Debug.WriteLine("Starting...") //This is successfully written out
//Websocket request is accepted and no exceptions are raised..
HttpContext.Current.AcceptWebSocketRequest(Function() Tasks.Task.FromResult(New ChatWebSocketHandler()))
//Let the client know we're upgrading...
Return Request.CreateResponse(HttpStatusCode.SwitchingProtocols)
Else
//Handle any bad requests
Return Request.CreateResponse(HttpStatusCode.BadRequest)
End If
End Function
//This is my websocket handler - using the Microsoft Websockets class.
Public Class ChatWebSocketHandler
Inherits Microsoft.AspNet.SignalR.WebSockets.WebSocketHandler
//This constructor is hit when the handshake happens, as expected.
Public Sub New()
MyBase.New(20000)
End Sub
//Everything from here down never gets hit, despite the client
//side onOpen/onClose event handlers being raised and readyState being 1.
Public Overrides Sub OnOpen()
//Breakpoints on either line below are not hit
MyBase.OnOpen()
Debug.WriteLine("Websocket is open")
End Sub
//I would expect this to get hit when websocket.send is called,
//no such luck.
Public Overrides Sub OnMessage(message As String)
Debug.WriteLine("Message Received: " & message)
End Sub
//never hit
Public Overrides Sub OnError()
MyBase.OnError()
Debug.WriteLine("Websocket is open")
End Sub
//never hit
Public Overrides Sub OnClose()
Debug.WriteLine("Websocket Closed")
MyBase.OnClose()
End Sub
End Class
End Class
成功握手:
当运行时,握手似乎是成功的,因为这一行被击中(以及构造函数),并且没有错误:
//Server Side portion of handshake:
HttpContext.Current.AcceptWebSocketRequest(Function()
Tasks.Task.FromResult(New ChatWebSocketHandler()))
//Finish the handshake, let the browser know we're upgrading
Return Request.CreateResponse(HttpStatusCode.SwitchingProtocols)
点击客户端onOpen()函数,写出readystate为1。然而,由于某种原因,当onOpen()中的第二行被击中时,websocket立即关闭(它跳转到onClose()处理程序)
//Client side handlers:
function onOpen(evt) {
writeToScreen("ONOPEN READYSTATE: " + websocket.readyState);
websocket.send("Websocket's open!")
}
function onClose(evt) {
writeToScreen("Socket closed");
}
:
ChatWebSocketHandler.OnOpen()(见底部的服务器代码)永远不会被命中。实际上,该类中的方法(OnClose(), OnError()等)除了握手期间的构造函数外,都不会被命中。为什么这些不命中?
注意,如果我在websocket.send()行上设置了一个断点,在该行执行之前,readystate仍然是1,并且在该行发生NOT错误,它只是跳转到websocket.onClose()事件…
提前感谢您的帮助!如果需要,我可以发布更多的客户端
我终于弄明白了,当然这是一些愚蠢的东西:)
显然,我犯了在SignalR中引用WebSocketHandler类的错误-它看起来不像是要在SignalR的内部操作之外使用的。
我的新代码非常相似,实际上唯一的区别是继承的websockethandler类在Microsoft.Web.Websockets命名空间中。
另一个区别是这一行:
HttpContext.Current.AcceptWebSocketRequest(Function() Tasks.Task.FromResult(New ChatWebSocketHandler()))
现在正好:
HttpContext.Current.AcceptWebSocketRequest(New ChatWebSocketHandler)
花了2天的时间到达那里,现在我和PM团队在一起,因为我们赶时间,但是,嘿,终于到了那里。希望如果有人碰到这个,他们不会像我一样被困在上面!