当前位置: 首页 > 编程语言 > C#

SignalR-Theconnectionmustbestartedbeforedatacanbesent.在.send()共享之前调用.start()

时间:2023-04-10 21:48:59 C#

SignalR:必须在发送数据之前启动连接。在.send()之前调用.start()直到最近才开始使用SignalR,我几天前就开始使用它了,现在不知道出了什么问题。我有我的中心:[HubName("roleManagementHub")]publicclassRoleManagementHub:GenericHub{publicRoleManagementHub(ICurrentlyViewingUserServiceservice):base(service){}}这个扩展了我创建的通用集线器:publicclassGenericHub:HubwhereTEntity:class,IBusinessObject{privatereadonlyICurrentlyViewingUserService服务;公共GenericHub(ICurrentlyViewingUserService服务){this.service=service;}publicvoidImViewing(stringid){字符串颜色;service.AddCurrentlyViewingUser(id,HttpContext.Current.User.Identity.Name,颜色);Clients.handleImViewingMessage(HttpContext.Current.User.Identity.Name,颜色,id);}publicvoidImLeaving(stringid){service.RemoveCurrentlyViewingUser(id,HttpContext.Current.User.Identity.Name);Clients.handleImLeavingMessage(HttpContext.Current.User.Identity.Name,id);}publicvoidIHaveEdited(stringid,stringproperty,stringcontent){stringcolor=service.GetCurrentlyViewingUserColour(id,HttpContext.Cur租金.User.Identity.Name);如果(字符串。IsNullOrEmpty(颜色))返回;Clients.handleIHaveEdited(id,属性,内容);我将Now包含在主要的javascript中。我创建了一个可重用的KnockoutJS组件,它接收一个集线器以连接到关联的处理程序。很多都是无关紧要的,但我想我会把它全部贴出来。我在闭包的开头开始我的连接,并在roleManagementHub中创建传递的敲除组件。varuser_role_edit={};(function(index){user_role_edit=index;jQuery.connection.hub.start();varval=ko.setupValidation([],[]);val.createErrorCollections({Name:0,General:0},index,'rename-role-form');vardmp=newdiff_match_patch();index.Id=ko.observable();index.Name=ko.observable();index.currentViewersViewModel=newko.currentlyViewingComponent.viewModel({hub:jQuery.connection.roleManagementHub,id:index.Id,modalSelector:'#user-role-edit-modal'});index.rename=function(form){jQuery.post('"+@Url.Action("重命名","RoleManagement")+@"',{'id':index.Id(),'name':index.Name()},function(dataReturned){if(dataReturned.IsValid){jQuery(document).trigger('userRoleUpdated',index);index.editModal.modal('hide');}else{val.rebindValidations({Name:0,General:0},index,dataReturned.Errors);}});};index.raiseClose=function(){index.editModal.modal('hide');};index.raiseDetails=function(){jQuery(文档).trigger('userRoleDetails',[index]);index.editModal.modal('隐藏');}jQuery(document).bind('userRoleEdit',function(event,id){jQuery.getJSON('"+@Url.Action("GetNewsArticleForUpdateOrDelete","RoleManagement")+@"',{id:id},函数(数据){index.Id(data.Role.Id);index.Name(data.Role.Name);index.currentViewersViewModel.initialiseCurrentViewers(data.CurrentlyViewingUsers);val.clearValidations({Name:0,General:0},index);index.editModal.modal('show');});});jQuery.connection.roleManagementHub.handleIHaveEdited=function(id,property,content){if(index.Id()!=id)返回;如果(索引[属性]==未定义)返回;dmp.Match_Distance=1000;dmp.Match_Threshold=0.5;dmp.Patch_DeleteThreshold=0.5;varpatches=dmp.patch_make(index[property](),content);varresults=dmp.patch_apply(patches,index[property]());指数[属性](结果[0]);};jQuery(function(){ko.applyBindings(index,jQuery('#user-role-edit-容器')[0]);index.editModal=jQuery('#user-role-edit-modal').modal({backdrop:true,closeOnEscape:true,modal:true,show:false});jQuery('#rename-role-form>fieldset>div>div>input#Name').blur(function(event){jQuery.connection.roleManagementHub.iHaveEdited(index.Id(),'Name',index.Name());});});}(user_role_edit));");这是淘比赛组:(function(){functioncurrentUserViewModel(username,colour){this.Username=username;this.Colour=colour;}ko.currentlyViewingComponent={//定义视图可用于填充网格viewModel的模型类:ko.utils.arrayForEach(currentUsers,function(item){index.currentViewers.push(newcurrentUserViewModel(item.Username,item.Colour));});};configuration.hub.handleImViewingMessage=function(username,colour,id){if(configuration.id()!=id)return;varfindResult=ko.utils.arrayFirst(index.currentViewers(),function(item){returnitem.Username==username;});if(findResult==null)index.currentViewers.push(newcurrentUserViewModel(username,colour));jQuery('a[rel=tooltip]').tooltip();};configuration.hub.handleImLeavingMessage=function(username,id){if(configuration.id()!=id)return;index.currentViewers.remove(function(item){returnitem.Username==username;});};jQuery(configuration.modalSelector).bind('show',function(){configuration.hub.imViewing(configuration.id());});jQuery(configuration.modalSelector).bind('隐藏',function(){configuration.hub.imLeaving(configuration.id());});}};//用于渲染网格的模板vartemplateEngine=newko.nativeTemplateEngine();templateEngine.addTemplate=function(templateName,templateMarkup){document.write(""+templateMarkup+"");};庙会ateEngine.addTemplate("ko_currentlyViewing","");ko.bindingHandlers.currentlyViewingComponent={init:function(){return{'controlsDescendantBindings':true};},//调用此方法来初始化节点,如果更改网格绑定更新的内容,也会再次调用此方法:function(element,viewModelAccessor,allBindingsAccessor){varviewModel=viewModelAccessor(),allBindings=allBindingsAccessor();//清空元素while(element.firstChild)ko.removeNode(element.firstChild);//允许覆盖默认模板varcurrentlyViewingTemplateName=allBindings.currentlyViewingTemplate||“ko_currentlyViewing”;//渲染主网格varcurrentlyViewingContainer=element.appendChild(document.createElement("DIV"));ko.renderTemplate(currentlyViewingTemplateName,viewModel,{templateEngine:templateEngine},currentlyViewingContainer,"replaceNode");}};})();正如您在模式显示事件中看到的那样,它使用集线器发送消息,告诉其他连接用户用户已经打开开始查看该项目但我现在看到“SignalR:必须在发送数据之前启动连接。在.send()之前调用.start()”。我已经连接了!即使我尝试;jQuery(function(){jQuery.connection.hub.start();jQuery.connection.userManagementHub.imViewing("1");});我收到“连接必须启动才能发送数据”消息。有任何想法吗??提前致谢!Jon对start()的调用不是即时的,可能是导致问题的原因。您可能希望将启动后应该发生的代码移动到start方法中的回调,例如:以上是C#学习教程:SignalR:Theconnectionmustbestartedbeforedatacanbesent。在.send()之前调用.start()以共享所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——jQuery(function(){jQuery.connection.hub.start(function(){jQuery.connection.userManagementHub.imViewing("1");});});本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: