我正在尝试在Mono解决方案上编写一个SignalR自托管服务器的简单示例,以演示给我们的执行部门。我已经遵循了github示例代码,并且我有一半的功能正常工作。按下“广播”按钮后,文本框中的消息将在服务器控制台中回显,但不会广播回客户端。
服务器代码:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
//Initialize your application
new AppHost().Init();
WebApplication.Start<Startup>("http://127.0.0.1:10000");
}
protected void Application_BeginRequest(object src, EventArgs e)
{
this.Context.Response.AddHeader("Access-Control-Allow-Headers", "accept,origin,authorization,content-type");
#if DEBUG
Profiler.Start();
#endif
}
protected void Application_EndRequest(object src, EventArgs e)
{
#if DEBUG
Profiler.Stop();
#endif
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapHubs(new HubConfiguration { EnableCrossDomain = true });
}
}
public class Chat : Hub
{
public void Send(string message)
{
Console.WriteLine(message);
Clients.All.addMessage(message);
}
}
}
客户端代码:
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"> </script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<!-- If this is an MVC project then use the following -->
<!-- <script src="~/signalr/hubs" type="text/javascript"></script> -->
<script src="http://127.0.0.1:10000/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.connection.hub.url = "http://127.0.0.1:10000/signalr";
$.connection.hub.logging = true;
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
// Start the connection
$.connection.hub.start({ jsonp: true }).done(function() {
$("#broadcast").click(function () {
// Call the chat method on the server
chat.server.send($('#msg').val());
});
});
});
</script>
<div>
<input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />
<ul id="messages">
</ul>
</div>
当我启动客户端时,我在协商过程中遇到一个奇怪的错误,这似乎很奇怪,因为客户端可以成功地将消息推送到服务器。
[17:13:54 GMT-0400 (EDT)] SignalR: Auto detected cross domain url. jquery.signalR.min.js:10
[17:13:54 GMT-0400 (EDT)] SignalR: Negotiating with 'http://127.0.0.1:10000/signalr/negotiate'. jquery.signalR.min.js:10
[17:13:54 GMT-0400 (EDT)] SignalR: SignalR: Initializing long polling connection with server. jquery.signalR.min.js:10
[17:13:54 GMT-0400 (EDT)] SignalR: Attempting to connect to 'http://127.0.0.1:10000/signalr/connect?transport=longPolling&connectionToke…kjVpNzqCLDXzDDRkE&connectionData=%5B%7B%22name%22%3A%22chat%22%7D%5D&tid=2' using longPolling. jquery.signalR.min.js:10
GET http://127.0.0.1:10000/signalr/connect?transport=longPolling&connectionToke…D&tid=2&callback=jQuery191021586751635186374_1366665233871&_=1366665233874 500 (Internal Server Error) jquery-1.9.1.min.js:5
send jquery-1.9.1.min.js:5
b.extend.ajax jquery-1.9.1.min.js:5
s jquery.signalR.min.js:10
(anonymous function) jquery.signalR.min.js:10
[17:13:54 GMT-0400 (EDT)] SignalR: Longpolling connected
非常感谢任何帮助。