我有一个MVC 3,VB.NET,Razor应用程序使用SignalR进行聊天和系统消息系统...聊天工作完美无缺,但我希望能够在我的管理控制器中添加一个功能,到集线器的消息,然后集线器会正常运行,好像Javascript已从视图中调用它...集线器设置如下:
Imports SignalR.Hubs
Imports SignalR
Namespace SingalRTest
Public Class Chat
Inherits Hub
Public Sub Send(ByVal clientName As String, ByVal message As String)
'Call the addMessage method on all clients.
Clients.addMessage(clientName, message)
End Sub
End Class
End Namespace
我想过简单地使用NEW但不会工作,因为据我所知,集线器的实例必须保持完整..
我想做的是这样的事情:
Public Function notification(ByVal systemMessage as string)
Dim y As SingalRTest.Chat = Nothing
y.Send(User.Identity.Name.ToString, systemMessage)
Return RedirectToAction("Index", "Admin")
End Function
这根本不起作用,错误说:
Object reference not set to an instance of an Object
当它到达y.Send行...
答案 0 :(得分:3)
Imports SignalR.Hubs
Imports SignalR
Namespace SingalRTest
Public Class Chat
Inherits Hub
Public Sub Send(ByVal clientName As String, ByVal message As String)
'Call the addMessage method on all clients.
Clients.addMessage(clientName, message)
End Sub
Friend Shared Sub SendMessage(message As String)
Dim context As IHubContext = GlobalHost.ConnectionManager.GetHubContext(Of Chat)()
context.Clients.addMessage("System Message", message)
End Sub
End Class
End Namespace
然后,只要我想在窗口中发送通知,我只需将此代码放在控制器函数中,以便在所有客户端上调用addMessage方法:
SingalRTest.Chat.SendMessage("Testing Only")
也许这会帮助别人。