我刚刚开始使用 SignalR,并且能够毫无问题地将消息广播到所有连接的客户端。我现在想让我的客户(javascript)加入一个组并只接收该组的消息。在我的集线器中,我有以下方法可以将连接添加到命名组:
public Task JoinGroup(string groupName)
{
return this.Groups.Add(Context.ConnectionId, groupName);
}
在客户端,我有以下 JS:
var latLonBounds = new google.maps.LatLngBounds();
var tripIdentifier = '@Model.TripIdentifier';
$(function () {
var hub = $.connection.locationHub;
hub.client.updateLocation = function (coordinates) {
// Implementation omitted
});
latLonBounds.extend(position);
map.fitBounds(latLonBounds);
};
$.connection.hub.logging = true;
$.connection.hub.start().done(function() {
hub.server.joinGroup(tripIdentifier);
});
});
当我的页面在 Chrome 中加载时,我在控制台中看到以下 URL 的 HTTP 500:
http://localhost:49914/signalr/send?transport=serverSentEvents&connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAA8OmZGDrRfEaOjaHahkkarwAAAAACAAAAAAAQZgAAAAEAACAAAADM78kCBInmZa7OROdPoXraiugBXLR5Xu3htxYC2JSnSAAAAAAOgAAAAAIAACAAAAAZwnPx6GMKOiw%2FivqQPlSfCb4WNP342YxvGyBpalmjSDAAAADljl1vg%2F7GYtD3R4AA2A9LXEnNZkyQVQjDUrW7ZVbvfPpWz1GCcOn6aw5yrkrWFrhAAAAAyX5otz0Xhx8tuIRgX2Pr1b9ZWFMvoairvqzns1%2FhjN%2BRhDIuAqAfonTbsZDjkdyDAZXzAKQqwTEKhpa5LYBhKg%3D%3D
在 Visual Studio 中,我看到以下堆栈跟踪:
SignalR exception thrown by Task: System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Microsoft.AspNet.SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor descriptor)
at Microsoft.AspNet.SignalR.Hubs.DefaultHubManager.ResolveHub(String hubName)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.CreateHub(IRequest request, HubDescriptor descriptor, String connectionId, StateChangeTracker tracker, Boolean throwIfFailedToCreate)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data)
at Microsoft.AspNet.SignalR.PersistentConnection.<>c__DisplayClassa.<>c__DisplayClassc.<ProcessRequest>b__7()
at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func)
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Microsoft.AspNet.SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor descriptor)
at Microsoft.AspNet.SignalR.Hubs.DefaultHubManager.ResolveHub(String hubName)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.CreateHub(IRequest request, HubDescriptor descriptor, String connectionId, StateChangeTracker tracker, Boolean throwIfFailedToCreate)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest request, String connectionId, String data)
at Microsoft.AspNet.SignalR.PersistentConnection.<>c__DisplayClassa.<>c__DisplayClassc.<ProcessRequest>b__7()
at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func)<---
如果我从我的 JS 中删除对的调用hub.server.joinGroup(tripIdentifier);
,那么我不会得到 HTTP 500/异常,所以这行似乎是异常的来源。
任何关于我可以做些什么来使团体注册工作的建议将不胜感激。
谢谢!