我的 PC 上有一个控制台应用程序,它是 signalR 服务器。
我有一个 html 页面,它是 Internet 上的 signalR 客户端。但是我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器关闭,我有另一个响应。
signalR 客户端是否可以连接 PC 上的服务器?
(对不起我的英语不好)
我的页面
<input type="text" id="msg" value=" " />
<input type="button" id="send" value="send" />
<ul id="message">
</ul>
<script src="Scripts/jquery-1.6.4.min.js"></script>
<script src="Scripts/jquery.signalR-1.1.2.min.js"></script>
<script>
$(function () {
jQuery.support.cors = true;
// Open a connection to the remote server
var connection = $.hubConnection('http://MyIpAddress:8080');
var chat = connection.createHubProxy('chat');
chat.on('send', function (message) {
$('#message').append('<li>' + message + '</li>');
});
// Turn logging on so we can see the calls in the browser console
connection.logging = true;
connection.start().done(function () {
$('#send').click(function () {
chat.invoke('send', $('#msg').val());
});
});
});
</script>
我的服务器代码
using Microsoft.Owin.Hosting;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
//Install-Package Microsoft.Owin.Hosting -pre
//Install-Package Microsoft.Owin.Host.HttpListener -pre
namespace TestServer
{
class Program
{
static void Main(string[] args)
{
LocalIPAddress();
string url = "http://localhost:8080";
using (WebApplication.Start<Startup>(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
}
启动.cs
using Microsoft.AspNet.SignalR;
using Owin;
namespace TestServer
{
public class Startup
{
// This method name is important
public void Configuration(IAppBuilder app)
{
var config = new HubConfiguration
{
EnableCrossDomain = true
};
app.MapHubs(config);
}
}
}
聊天.cs
using Microsoft.AspNet.SignalR;
using System;
using System.Threading.Tasks;
namespace TestServer.Hubs
{
public class Chat : Hub
{
public override Task OnConnected()
{
Console.WriteLine("OnConnected " + Context.ConnectionId);
return base.OnConnected();
}
public void Send(string message)
{
Console.WriteLine(string.Format("Receive: {0} by {1} ", message, Context.ConnectionId));
Clients.All.send(message);
}
}
}