在 linqpad 中自托管 SignalR
Self hosting SignalR in linqpad
场景
我正在尝试使用 linqpad 自行托管 SignalR。
脚本
// I have uploaded to instant share here: http://share.linqpad.net/578ol2.linq
// Import the following nuget packages:
// Microsoft.AspNet.SignalR.SelfHost
// Microsoft.Owin.Cors
#define NONEST
void Main()
{
var baseUrl = @"http://localhost:8080/";
using (WebApp.Start<Startup>(baseUrl))
{
Process.Start(baseUrl + "index.html");
Console.WriteLine("Server running at " + baseUrl);
Console.ReadLine();
}
}
public class Startup
{
public readonly string Html = @"
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
</head>
<body>
<div class='container'>
<input type='text' id='message' />
<input type='button' id='sendmessage' value='Send' />
<input type='hidden' id='displayname' />
<ul id='discussion'></ul>
</div>
<script src='https://code.jquery.com/jquery-1.6.4.js'></script>
<script src='http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.js'></script>
<script src='http://localhost:8080/signalr/hubs'></script>
<script type='text/javascript'>
$(function () {
$.connection.hub.url = 'http://localhost:8080/signalr';
$.connection.hub.logging = true;
var chat = $.connection.myHub;
chat.client.addMessage = function (name, message) {
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
$('#discussion').append('<li><strong>' + encodedName + '</strong>: ' + encodedMsg + '</li>');
};
$('#displayname').val(prompt('Enter your name:', ''));
$('#message').focus();
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
chat.server.send($('#displayname').val(), $('#message').val());
$('#message').val('').focus();
});
});
});
</script>
</body>
</html>
".Trim();
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
app.Map("/index.html", config =>
{
config.Run(context =>
{
context.Response.ContentType = "text/html";
return context.Response.WriteAsync(Html);
});
});
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
结果
如果您查看浏览器控制台,您会发现 SignalR 最终会耗尽所有传输,因此无法在客户端和服务器之间创建连接
SignalR: Client subscribed to hub 'myhub'.
Negotiating with 'http://localhost:8080/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D'.
serverSentEvents transport starting.
Attempting to connect to SSE endpoint 'http://localhost:8080/signalr/connect?transport=serverSentEvents&clientProt…joHW8a9m5Q%3D%3D&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D&tid=1'.
EventSource connected.
serverSentEvents transport timed out when trying to connect.
EventSource calling close().
serverSentEvents transport failed to connect. Attempting to fall back.
foreverFrame transport starting.
Forever Frame is not supported by SignalR on browsers with SSE support.
foreverFrame transport failed to connect. Attempting to fall back.
longPolling transport starting.
Opening long polling request to 'http://localhost:8080/signalr/connect?transport=longPolling&clientProtocol=…QeRv75joHW8a9m5Q%3D%3D&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D'.
longPolling transport timed out when trying to connect.
Aborted xhr request.
longPolling transport failed to connect. Attempting to fall back.
Fallback transports exhausted.
Stopping connection.
Fired ajax abort async = true.
但是,同样的代码在控制台应用程序中可以正常工作
问题
有谁知道为什么这段代码在 linqpad 中托管时失败但在控制台应用程序中托管时有效?
我倾向于某种连接问题,而不是 LinqPad 问题,因为我能够 运行 毫无问题地剪断您的代码。我也在第 5 版下 运行 安装它。你碰巧有 3r 派对防火墙吗?
只是为了确认一下,预期的行为是什么?当我 运行 它时,我的浏览器弹出来要求我输入一个名字。然后我可以发送 'messages',它出现在文本框下方的页面上。
场景
我正在尝试使用 linqpad 自行托管 SignalR。
脚本
// I have uploaded to instant share here: http://share.linqpad.net/578ol2.linq
// Import the following nuget packages:
// Microsoft.AspNet.SignalR.SelfHost
// Microsoft.Owin.Cors
#define NONEST
void Main()
{
var baseUrl = @"http://localhost:8080/";
using (WebApp.Start<Startup>(baseUrl))
{
Process.Start(baseUrl + "index.html");
Console.WriteLine("Server running at " + baseUrl);
Console.ReadLine();
}
}
public class Startup
{
public readonly string Html = @"
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
</head>
<body>
<div class='container'>
<input type='text' id='message' />
<input type='button' id='sendmessage' value='Send' />
<input type='hidden' id='displayname' />
<ul id='discussion'></ul>
</div>
<script src='https://code.jquery.com/jquery-1.6.4.js'></script>
<script src='http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.js'></script>
<script src='http://localhost:8080/signalr/hubs'></script>
<script type='text/javascript'>
$(function () {
$.connection.hub.url = 'http://localhost:8080/signalr';
$.connection.hub.logging = true;
var chat = $.connection.myHub;
chat.client.addMessage = function (name, message) {
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
$('#discussion').append('<li><strong>' + encodedName + '</strong>: ' + encodedMsg + '</li>');
};
$('#displayname').val(prompt('Enter your name:', ''));
$('#message').focus();
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
chat.server.send($('#displayname').val(), $('#message').val());
$('#message').val('').focus();
});
});
});
</script>
</body>
</html>
".Trim();
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
app.Map("/index.html", config =>
{
config.Run(context =>
{
context.Response.ContentType = "text/html";
return context.Response.WriteAsync(Html);
});
});
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
结果
如果您查看浏览器控制台,您会发现 SignalR 最终会耗尽所有传输,因此无法在客户端和服务器之间创建连接
SignalR: Client subscribed to hub 'myhub'.
Negotiating with 'http://localhost:8080/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D'.
serverSentEvents transport starting.
Attempting to connect to SSE endpoint 'http://localhost:8080/signalr/connect?transport=serverSentEvents&clientProt…joHW8a9m5Q%3D%3D&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D&tid=1'.
EventSource connected.
serverSentEvents transport timed out when trying to connect.
EventSource calling close().
serverSentEvents transport failed to connect. Attempting to fall back.
foreverFrame transport starting.
Forever Frame is not supported by SignalR on browsers with SSE support.
foreverFrame transport failed to connect. Attempting to fall back.
longPolling transport starting.
Opening long polling request to 'http://localhost:8080/signalr/connect?transport=longPolling&clientProtocol=…QeRv75joHW8a9m5Q%3D%3D&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D'.
longPolling transport timed out when trying to connect.
Aborted xhr request.
longPolling transport failed to connect. Attempting to fall back.
Fallback transports exhausted.
Stopping connection.
Fired ajax abort async = true.
但是,同样的代码在控制台应用程序中可以正常工作
问题
有谁知道为什么这段代码在 linqpad 中托管时失败但在控制台应用程序中托管时有效?
我倾向于某种连接问题,而不是 LinqPad 问题,因为我能够 运行 毫无问题地剪断您的代码。我也在第 5 版下 运行 安装它。你碰巧有 3r 派对防火墙吗?
只是为了确认一下,预期的行为是什么?当我 运行 它时,我的浏览器弹出来要求我输入一个名字。然后我可以发送 'messages',它出现在文本框下方的页面上。