2013-04-13 34 views
0

我有一个5.3.0版本的signalr自托管,正在升级到更新版本的signalr。 使用https://github.com/SignalR/SignalR/wiki/Self-host示例我创建了一个简单的示例,但我无法使其工作。Signalr Owin简单示例javascript客户端不被称为

我可以连接到服务器上的集线器,并调用集线器上的方法,但我无法让集线器调用JavaScript客户端。

当在小提琴手看它时,我从来没有看到从枢纽回来的回应。

下面是代码

​​

}

using System; 
using System.Threading.Tasks; 
using Microsoft.AspNet.SignalR; 
using Newtonsoft.Json; 

namespace ConsoleApplication3.Hubs 
{ 
public class Chat : Hub 
{ 

    public override Task OnConnected() 
    { 
     Notify(Context.ConnectionId); 
     return new Task(() => { }); 
    } 

    public void RunTest() 
    { 
     Notify(Context.ConnectionId); 
    } 


    public void Notify(string connectionId) 
    { 
     dynamic testMessage = new 
     { 
      Count = 3, 
      Message = "Some test message", 
      Timestamp = DateTime.Now 
     }; 

     String json = JsonConvert.SerializeObject(testMessage); 

     var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
     context.Clients.Client(connectionId).sendNotification(json); 
    } 

} 
} 

这里是客户端

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title>  
    <script src="Scripts/json2.js"></script> 
    <script src="Scripts/jquery-1.9.1.js"></script> 
    <script src="Scripts/jquery.signalR-1.0.1.js"></script> 
    <script src="http://localhost:8080/signalr/hubs"></script> 

<script>   
    $(function() { 

     // Proxy created on the fly 
     var notification = $.connection.chat; 
     $.connection.hub.logging = true; 

     // Declare functions that can be run on the client by the server 
     notification.client.sendNotification = onAddNotification; 
     notification.client.disconnected = function (connectionid) { 
      console.log(connectionid); 
     }; 
     // Testing code only 
     $("#testButton").click(function() { 
      // Run test function on server 
      notification.server.runTest(); 
     }); 

     jQuery.support.cors = true; 

     // Map the onConnect and onDisconnect functions 
     notification.client.connected = function() { 
      alert("Notification system connected"); 
     }; 
     notification.client.disconnected = function() { }; 
     $.connection.hub.url = "http://localhost:8080/signalr"; 

     //$.connection.hub.start(); 
     $.connection.hub.start(function() { 
      alert("Notification system connected"); 
     }); 

    }); 

    // Process a newly received notification from the server 
    function onAddNotification(message) { 

     // Convert the passed json message back into an object 
     var obj = JSON.parse(message); 

     var parsedDate = new Date(parseInt(obj.Timestamp.substr(6))); 

     // Update the notification list 
     $('#notifications').prepend('<li>' + obj.Message + ' at ' + parsedDate + '</li>'); 

    }; 

    </script> 
</head> 
<body>  
    <a href="javascript:void(0)" class="btn" id="testButton">Send test</a>  
    <ul class="unstyled" id="notifications">    
    </ul> 
</body> 

任何想法,将不胜感激,因为我相当卡住。

回答

2

代码中的几件事情:

更改此:

public override Task OnConnected() 
{ 
    Notify(Context.ConnectionId); 
    return new Task(() => { }); 
} 

要:

public override Task OnConnected() 
{ 
    Notify(Context.ConnectionId); 
    return base.OnConnected(); 
} 

而且在集线器:

此功能过于卖力:

public void Notify(string connectionId) 
{ 
    dynamic testMessage = new 
    { 
     Count = 3, 
     Message = "Some test message", 
     Timestamp = DateTime.Now 
    }; 

    String json = JsonConvert.SerializeObject(testMessage); 

    var context = GlobalHost.ConnectionManager.GetHubContext<Chat>(); 
    context.Clients.Client(connectionId).sendNotification(json); 
} 

我甚至不知道为什么你传递的连接ID(也许它的意思是静态的?)

public void Notify() 
{ 
    dynamic testMessage = new 
    { 
     Count = 3, 
     Message = "Some test message", 
     Timestamp = DateTime.Now 
    }; 

    Clients.Client(Context.ConnectionId).sendNotification(testMessage); 
} 

你并不需要序列化两次,我们已经为你做它。

删除:

jQuery.support.cors = true; 

决不设置。

另外:

// Map the onConnect and onDisconnect functions 
notification.client.connected = function() { 
    alert("Notification system connected"); 
}; 

notification.client.disconnected = function() { }; 

这些都不是什么映射客户端。您无法将连接和从服务器断开连接到客户端。客户有它自己的事件。

其他的事情:

这应该是开始回调的内部,使得它已经准备好之前,你不打它:

$.connection.hub.start().done(function() { 
    // Testing code only 
    $("#testButton").click(function() { 
     // Run test function on server 
     notification.server.runTest(); 
    }); 
}); 
+0

感谢大卫说做到了。我现在的问题是,我连接到两个集线器上不同的网址,当我这样做: - 'code' var cfgBrokerHubConnection = $ .hubConnection(cfgBrokerSignalRHubServer); var cfgBroker = cfgBrokerHubConnection。createHubProxy( 'cfgBrokerHub');我在这一行发生错误cfgBroker.client.receiveMessage = processCfgMessage(m);说客户端是未定义的。 –

+0

https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs-%28No-Proxy%29 – davidfowl

+0

我面对同样的OnConnected问题,但我不使用EnableJavaScriptProxies = true,我的代码是[here ](https://stackoverflow.com/questions/47138223/angular-signalr-error-during-negotiation-request)但解决了这个问题。现在获取连接Id但OnConnected不会被解雇:( – Developer