我无法让我的小型游乐场应用程序在我的 VPS 上顺利运行,该应用程序在开发机器上运行顺利,但一旦移动到 VPS,每个第一个请求都需要很长时间才能完成。
IIS 没有任何特定配置,DNS 托管在DNS Simple上。
我已经为应用程序分配了适当的应用程序池,甚至将池权限更改为 localSystem,但这并没有影响问题。
这是我在整个MVC4项目中唯一的 hub 类:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace SignalR_chat.Models
{
[HubName("chatHub")]
public class ChatHub : Hub
{
private static Dictionary<string, string> _userList = new Dictionary<string, string>();
public void AddText(string chatMessage)
{
var validUser = _userList.SingleOrDefault(s => s.Key == Context.ConnectionId);
var message = validUser.Value == string.Empty ? null :
string.Format("<li><b>{0}</b>: {1} </li>",
validUser.Value, HttpUtility.HtmlEncode(chatMessage));
if (message != null)
Clients.All.retrieveInput(message);
}
public void AddInUserList(string nickName, string connectionId)
{
_userList.Add(connectionId, nickName);
OnConnected();
}
public override Task OnDisconnected()
{
var userInstance = _userList.SingleOrDefault(x => x.Key == Context.ConnectionId);
if (userInstance.Value != string.Empty)
_userList.Remove(Context.ConnectionId);
OnConnected();
return null;
}
public override Task OnConnected()
{
var jsonObj = _userList.Count != -1
? new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(_userList)
: string.Empty;
if (jsonObj != string.Empty)
Clients.All.currentUsers(jsonObj);
return null;
}
}
}
这是在Gloabl.asax中注册的 Hubs 路由的方式
protected void Application_Start()
{
RouteTable.Routes.MapHubs();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
这是我处理信号器事件的 jQuery 代码
/* File Created: July 24, 2012 */
/// <reference path="jquery-2.0.0.min.js" />
/// <reference path="jquery.signalR-0.5.3.min.js" />
/// <reference path="shortcut.js" />
/// <reference path="bootbox.min.js" />
/**********************/
/* Client functions */
/**********************/
var Chathub = $.connection.chatHub;
Chathub.client.retrieveInput = function (val) {
$('.chatList').append(val);
$(function () {
var height = $('.chatBox')[0].scrollHeight;
$('.chatBox').scrollTop(height);
});
$(function () {
var height = $('#chat')[0].scrollHeight;
$('#chat').scrollTop(height);
});
};
Chathub.client.currentUsers = function (list) {
$('.userList').empty();
var json = $.parseJSON(list);
$.each(json, function (name, value) {
$('.userList').append("<li>" + value + "</li>");
});
};
/**********************/
/* Server functions */
/**********************/
$.connection.hub.start().done(function () {
$('.chatInput').click(function () {
if (!localStorage.getItem('nickName')) {
$('.chatInput').attr('disabled', 'disabled');
window.bootbox.prompt('Enter nickname please', function (temp) {
if (temp) {
if (temp.length <= 10) {
localStorage.setItem('nickName', temp);
Chathub.server.addInUserList(temp, $.connection.hub.id);
$('.chatInput').unbind();
} else {
window.bootbox.alert("nickname must be 10 characters or less");
}
}
});
$('.chatInput').removeAttr('disabled', 'disabled');
} else {
Chathub.server.addInUserList(localStorage.getItem('nickName'), $.connection.hub.id);
$('.chatInput').unbind();
}
});
$('.submitChat').click(function () {
if ($('.chatInput').val() != '' && localStorage.getItem('nickName')) {
Chathub.server.addText($('.chatInput').val());
$('.chatInput').val('');
}
});
});
shortcut.add("Enter", function () {
$('.submitChat').click();
});
$.connection.hub.stateChanged(function (change) {
if (change.newState === $.signalR.connectionState.reconnecting) {
$('.chatBox').addClass("Tilt");
$('.overlay').css('display', 'block');
} else if (change.newState === $.signalR.connectionState.connected) {
$('.chatBox').removeClass("Tilt");
$('.overlay').css('display', 'none');
}
});
这是完整的项目,据我所知,jQuery 中的连接正在同步处理,即 .done() 中的链接事件
最后一点,我只对 JS 库使用捆绑和缩小,但不包括“~/signalr/hubs”,以便在布局视图中保持单独链接。
提前致谢
编辑:忘了提到我正在使用 SignalR 1.0.1
Edit2:我试过用提琴手调试这个问题,但是我找不到任何具体的细节。我唯一注意到的是,在删除所有会话后的每个第一个请求中,我都会在 21 秒内得到响应,但之后的所有请求都会得到相当快的响应。以下是 fiddler 对结束所有会话后的请求和结束会话前的请求的统计信息。
新的会话统计
Request Count: 1
Bytes Sent: 332 (headers:332; body:0)
Bytes Received: 3,234 (headers:169; body:3,065)
ACTUAL PERFORMANCE
--------------
ClientConnected: 17:35:30.949
ClientBeginRequest: 17:35:30.949
GotRequestHeaders: 17:35:30.949
ClientDoneRequest: 17:35:30.949
Determine Gateway: 0ms
DNS Lookup: 0ms
TCP/IP Connect: 21102ms
HTTPS Handshake: 0ms
ServerConnected: 17:35:52.054
FiddlerBeginRequest: 17:35:52.054
ServerGotRequest: 17:35:52.054
ServerBeginResponse: 17:35:52.275
GotResponseHeaders: 17:35:52.275
ServerDoneResponse: 17:35:52.275
ClientBeginResponse: 17:35:52.275
ClientDoneResponse: 17:35:52.275
Overall Elapsed: 0:00:21.326
RESPONSE BYTES (by Content-Type)
--------------
text/html: 3,065
~headers~: 169
REQUESTS PER HOST
--------------
[removing this one]
ESTIMATED WORLDWIDE PERFORMANCE
--------------
The following are VERY rough estimates of download times when hitting servers based in WA, USA.
US West Coast (Modem - 6KB/sec)
RTT: 0.10s
Elapsed: 0.10s
Japan / Northern Europe (Modem)
RTT: 0.15s
Elapsed: 0.15s
China (Modem)
RTT: 0.45s
Elapsed: 0.45s
US West Coast (DSL - 30KB/sec)
RTT: 0.10s
Elapsed: 0.10s
Japan / Northern Europe (DSL)
RTT: 0.15s
Elapsed: 0.15s
China (DSL)
RTT: 0.45s
Elapsed: 0.45s
结束会话前的统计数据
Request Count: 1
Bytes Sent: 332 (headers:332; body:0)
Bytes Received: 3,234 (headers:169; body:3,065)
ACTUAL PERFORMANCE
--------------
ClientConnected: 17:37:56.795
ClientBeginRequest: 17:37:56.795
GotRequestHeaders: 17:37:56.795
ClientDoneRequest: 17:37:56.795
Determine Gateway: 0ms
DNS Lookup: 0ms
TCP/IP Connect: 0ms
HTTPS Handshake: 0ms
ServerConnected: 17:35:52.054
FiddlerBeginRequest: 17:37:56.795
ServerGotRequest: 17:37:56.795
ServerBeginResponse: 17:37:56.899
GotResponseHeaders: 17:37:56.899
ServerDoneResponse: 17:37:56.899
ClientBeginResponse: 17:37:56.899
ClientDoneResponse: 17:37:56.899
Overall Elapsed: 0:00:00.103
RESPONSE BYTES (by Content-Type)
--------------
text/html: 3,065
~headers~: 169
REQUESTS PER HOST
--------------
[removing this one]
ESTIMATED WORLDWIDE PERFORMANCE
--------------
The following are VERY rough estimates of download times when hitting servers based in WA, USA.
US West Coast (Modem - 6KB/sec)
RTT: 0.10s
Elapsed: 0.10s
Japan / Northern Europe (Modem)
RTT: 0.15s
Elapsed: 0.15s
China (Modem)
RTT: 0.45s
Elapsed: 0.45s
US West Coast (DSL - 30KB/sec)
RTT: 0.10s
Elapsed: 0.10s
Japan / Northern Europe (DSL)
RTT: 0.15s
Elapsed: 0.15s
China (DSL)
RTT: 0.45s
Elapsed: 0.45s
Edit3:我注意到 VS 中的以下输出。
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Internals\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Internals.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Data.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml.Hosting\v4.0_4.0.0.0__31bf3856ad364e35\System.Xaml.Hosting.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Workflow.ComponentModel\v4.0_4.0.0.0__31bf3856ad364e35\System.Workflow.ComponentModel.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Workflow.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.Workflow.Activities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Workflow.Runtime\v4.0_4.0.0.0__31bf3856ad364e35\System.Workflow.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.DurableInstancing\v4.0_4.0.0.0__31bf3856ad364e35\System.Runtime.DurableInstancing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml\v4.0_4.0.0.0__b77a5c561934e089\System.Xaml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.Build.Framework\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.Build.Framework.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.Entity\v4.0_4.0.0.0__b77a5c561934e089\System.Data.Entity.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\SMDiagnostics\v4.0_4.0.0.0__b77a5c561934e089\SMDiagnostics.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\Microsoft.VisualBasic.Activities.Compiler\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.Activities.Compiler.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mobile\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'Anonymously Hosted DynamicMethods Assembly'
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\XXX\AppData\Local\Temp\Temporary ASP.NET Files\root\c514b873\93518333\App_Web_lcqvz5d1.dll', Symbols loaded.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\XXX\AppData\Local\Temp\Temporary ASP.NET Files\root\c514b873\93518333\App_Web_l5dllaun.dll', Symbols loaded.
'iisexpress.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x275c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1f80) has exited with code 0 (0x0).
The thread '<No Name>' (0x1878) has exited with code 0 (0x0).
The thread '<No Name>' (0x2238) has exited with code 0 (0x0).
The thread '<No Name>' (0x69c) has exited with code 0 (0x0).
The thread '<No Name>' (0x21d0) has exited with code 0 (0x0).
The thread '<No Name>' (0xadc) has exited with code 0 (0x0).
The thread '<No Name>' (0x1278) has exited with code 0 (0x0).
The thread '<No Name>' (0x177c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1e90) has exited with code 0 (0x0).
The thread '<No Name>' (0x1988) has exited with code 0 (0x0).
The thread '<No Name>' (0x56c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1100) has exited with code 0 (0x0).
The thread '<No Name>' (0x22c4) has exited with code 0 (0x0).
The program '[7720] iisexpress.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
The program '[7720] iisexpress.exe: Program Trace' has exited with code 0 (0x0).
显然有些任务有一些例外。不确定这是我的代码还是其他问题