2013-03-07 140 views
3

我设计类似于谷歌使用SignalR连接文档实时文档编辑器的Web应用程序。Connection.Start()()完成SignalR问题

它好的工作也就是当我在浏览器中一个编辑在写,被显示在其他打开的浏览器我有文字。我遇到的唯一问题是,一开始我写一些文本时,它不会被显示,然后我再次删除并重新写入,并且一切正常。

当调试在Chrome中使用F12我收到此错误:

Uncaught Error: SignalR: Connection has not been fully initialized. Use .start().done() or  .start().fail() to run logic after the connection has started. 

我不明白这一点,因为在我的代码,我实际使用$ .connection.hub.start.done()。这里是我使用的集线器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Microsoft.AspNet.SignalR; 
using Microsoft.AspNet.SignalR.Hubs; 

namespace Write.ly 
{ 
    [HubName("editor")] 
    public class EditorHub : Hub 
    { 
     public void Send(string message) 
     { 
      Clients.Others.broadcastMessage(message); 
     } 
    } 
} 

这是与此相关的JavaScript和html。请注意,我正在使用tinyMCE作为编辑器的插件。

@{ 
    ViewBag.Title = "- Editor"; 
    ViewBag.ContentStyle = "/Content/CSS/editor.css"; 
} 

<script src="~/Scripts/jquery.signalR-1.0.1.min.js"></script> 
<script src="~/signalr/hubs"></script> 
<script src="~/Content/TinyMCE/tiny_mce.js"></script> 
<script type="text/javascript"> 
    $(function() { 
     var hub = $.connection.editor; 

     tinyMCE.init({ 
      mode: "textareas", 
      theme: "advanced", 
      plugins: "emotions,spellchecker,advhr,insertdatetime,preview", 

      // Theme options - button# indicated the row# only 
      theme_advanced_buttons1: "newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect,formatselect", 
      theme_advanced_buttons2: "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,code,preview,|,forecolor,backcolor", 
      theme_advanced_buttons3: "insertdate,inserttime,|,spellchecker,advhr,,removeformat,|,sub,sup,|,charmap,emotions", 
      theme_advanced_toolbar_location: "top", 
      theme_advanced_toolbar_align: "left", 
      theme_advanced_statusbar_location: "bottom", 
      theme_advanced_resizing: false, 

      setup: function (ed) { 
       ed.onKeyUp.add(function (ed, e) { 
        hub.client.broadcastMessage = function (message) { 
         var bookmark = ed.selection.getBookmark(2, true); 
         tinyMCE.activeEditor.setContent(message); 
         ed.selection.moveToBookmark(bookmark); 
        }; 

        $.connection.hub.start().done(function() { 
         var text = tinyMCE.activeEditor.getContent(); 
         hub.server.send(text); 
        }); 
       }); 
      } 
     }); 
    }); 
</script> 

<form method="post" action="somepage"> 
     <textarea id="editor" name="content" cols="100" rows="30"></textarea> 
</form> 

<button class="btn" onclick="ajaxSave();"><span>Save</span></button> 

任何想法?

回答

5

您应该只进行一次开始你的SignalR连接,而不是在每一个KEYUP。在开始连接之前,您还应该创建您的客户端集线器方法:

<script type="text/javascript"> 
    $(function() { 
     var hub = $.connection.editor; 

     tinyMCE.init({ 
      mode: "textareas", 
      theme: "advanced", 
      plugins: "emotions,spellchecker,advhr,insertdatetime,preview", 

      // Theme options - button# indicated the row# only 
      theme_advanced_buttons1: "newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect,formatselect", 
      theme_advanced_buttons2: "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,code,preview,|,forecolor,backcolor", 
      theme_advanced_buttons3: "insertdate,inserttime,|,spellchecker,advhr,,removeformat,|,sub,sup,|,charmap,emotions", 
      theme_advanced_toolbar_location: "top", 
      theme_advanced_toolbar_align: "left", 
      theme_advanced_statusbar_location: "bottom", 
      theme_advanced_resizing: false, 

      setup: function (ed) { 
       hub.client.broadcastMessage = function (message) { 
        var bookmark = ed.selection.getBookmark(2, true); 
        tinyMCE.activeEditor.setContent(message); 
        ed.selection.moveToBookmark(bookmark); 
       }; 

       $.connection.hub.start().done(function() { 
        ed.onKeyUp.add(function (ed, e) { 
         var text = tinyMCE.activeEditor.getContent(); 
         hub.server.send(text); 
        }); 
       }); 
      } 
     }); 
    }); 
</script> 
+0

非常感谢!不知道我每次启动连接都会启动连接!难怪!当我在tinyMCE中设置粗体或字体大小的文本时,是否碰巧想知道如何让其他客户端上的文本发生更改?我认为这应该与上面的代码一起工作,因为它应该用html标签发送文本。任何想法? – Bernice 2013-03-07 20:28:20