1

注意: 我用 C# 标记它,因为我相信它会在 ASP.NET 社区中影响更广泛的受众。如果这是一个问题,请告诉我,我可以删除它(C# 是我的首选语言,但对于这个项目我'我被迫用VB编写)。websocket 服务器是用 VB.NET ASP.NET WebApi 编写的。任何帮助将不胜感激。

我只是想让连接打开,保持打开状态,然后从客户端向服务器发送一条快速字符串消息。我已将我的服务器代码简化为尽可能简单,并在下面完整发布。


以下是我的服务器代码: 注意:我将注释从 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,并且在该行不会出错,它只是跳转到websocket.onClose()事件。 .

预先感谢您的帮助!如果需要,我可以发布更多的客户端。

4

1 回答 1

2

好吧,我终于想通了,当然这是愚蠢的:)

显然,我犯了在 SignalR 中引用 WebSocketHandler 类的错误——它看起来并不打算在 SignalR 的内部操作之外使用。

我的新代码非常相似,真正唯一的区别是继承的 websockethandler 类在 Microsoft.Web.Websockets 命名空间中。

另一个区别是这一行:

HttpContext.Current.AcceptWebSocketRequest(Function() Tasks.Task.FromResult(New ChatWebSocketHandler()))

现在只是:

HttpContext.Current.AcceptWebSocketRequest(New ChatWebSocketHandler)

花了 2 天时间到达那里,现在我和 PM 团队一起在狗屋里,因为我们很着急,但是,嘿,终于到了那里。希望如果其他人点击它,他们不会像我一样被卡住!

于 2016-10-14T17:36:00.063 回答