我正在使用Azure网站。在我的ASP Net MVC项目中,我创建了一个websocket服务器和客户端。当我试图打电话给我的服务器时,我得到了
与'ws://binfx.azurewebsites.net/WebSocketServer.ashx'的WebSocket连接失败:WebSocket握手期间出错:net :: ERR_CONNECTION_RESET
我的服务器类是:
WebSocketServer.ashx
public class WebSocketServer : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.IsWebSocketRequest)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
context.AcceptWebSocketRequest(new MicrosoftWebSockets());
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("Not a websocket response!");
context.Response.StatusCode=400;
}
}
}
我的客户代码是:
Index.cshtml
window.onload = function () {
var conversation = $('body');
var localHost = window.document.location.host;
var url = "ws://" + localHost + "/WebSocketServer.ashx";
if (window.WebSocket) {
var ws = new WebSocket(url);
ws.onmessage = function (response) {
conversation.append(createSpan('Response:' + response.data));
}
ws.onopen = function (response) {
ws.send('message!');
conversation.append(createSpan('Azure CONNECTED'));
}
ws.onerror = function (e) {
conversation.append(createSpan("There are connection issues"));
}
}
else {
alert("Your browser does not support instant messaging. You need to regularly refresh your browser in order to receive updates");
}
}
我已启用在Azure门户中使用Websockets
Enabling using Websockets in Azure Portal
感谢您的想法!