我在使用JS客户端访问SignalR Hub时遇到问题。 这是我的中心
class Program
{
static void Main(string[] args)
{
string url = "http://192.168.0.9:8085";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
[HubName("MyServerHub")]
public class MyServerHub : Hub
{
public void Subscribe(string collectionName)
{
Groups.Add(Context.ConnectionId, collectionName);
Console.WriteLine("Subscribed to: " + collectionName);
}
public void BroadcastMessageToAll(string name, string message, string time)
{
Clients.All.broadcastMessageToAll(name, message, time);
}
public void JoinAGroup(string group)
{
Groups.Add(Context.ConnectionId, group);
}
public void RemoveFromAGroup(string group)
{
Groups.Remove(Context.ConnectionId, group);
}
public void BroadcastToGroup(string message, string group)
{
Clients.Group(group).newMessageReceived(message);
}
}
这是我的JS客户端
<html xmlns="http://www.w3.org/1999/xhtml">
<script src ="Scripts/json2.js" type="text/jscript"></script>
<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-0.5.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var connection = $.hubConnection('http://192.168.0.9:8085/');
proxy = connection.createProxy('MyServerHub')
connection.start()
.done(function () {
proxy.invoke('subscribe', 'Chrome client');
$('#messages').append('<li>Subscribed</li>');
})
.fail(function () { alert("Could not Connect!"); });
proxy.on('flush', function (msg) {
$('#messages').append('<li>' + msg + '</li>');
});
$("#broadcast").click(function () {
proxy.invoke('Publish', $("#dataToSend").val(), 'Product');
});
});
</script>
<body>
<div>
<ul id="messages"></ul>
<input type="text" id="dataToSend" />
<input id="broadcast" type="button" value="Send"/>
</div>
</body>
我的浏览器一直显示“无法连接”。我不知道为什么我的js客户端不起作用。如果有人能告诉我哪里出错了,我真的很感激。谢谢!