This simple web socket example返回200错误。
编辑:我在C#中重新发布代码,希望更多的人能够告诉我为什么我遇到这个问题。
我正在运行VS2012 Express,在我的本地IIS机器上,项目配置为4.5.1框架,我已经导入了Nuget Microsoft.Websockets包。
我在下面列出的三段代码是项目中仅有的三段代码,我没有对项目的其余部分进行任何修改。
在意外错误发生之前没有中断,它在打开时或在任何一方的消息上都不会中断。 200在Chrome控制台中出现错误,但没有响应预览。
这是客户端(index.htm):
<!doctype html>
<html>
<head>
<title></title>
<script src="Scripts/jquery-1.8.1.js" type="text/javascript"></script>
<script src="test.js" type="text/javascript"></script>
</head>
<body>
<input id="txtMessage" />
<input id="cmdSend" type="button" value="Send" />
<input id="cmdLeave" type="button" value="Leave" />
<br />
<div id="chatMessages" />
</body>
</html>
和客户端脚本(test.js):
$(document).ready(function () {
var name = prompt('what is your name?:');
var url = 'ws://' + window.location.hostname + window.location.pathname.replace('index.htm', 'ws.ashx') + '?name=' + name;
alert('Connecting to: ' + url);
var ws = new WebSocket(url);
ws.onopen = function () {
$('#messages').prepend('Connected <br/>');
$('#cmdSend').click(function () {
ws.send($('#txtMessage').val());
$('#txtMessage').val('');
});
};
ws.onmessage = function (e) {
$('#chatMessages').prepend(e.data + '<br/>');
};
$('#cmdLeave').click(function () {
ws.close();
});
ws.onclose = function () {
$('#chatMessages').prepend('Closed <br/>');
};
ws.onerror = function (e) {
$('#chatMessages').prepend('Oops something went wrong<br/>');
};
});
这是通用处理程序(ws.ashx):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Web.WebSockets;
namespace WebSockets
{
public class ws : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.IsWebSocketRequest)
context.AcceptWebSocketRequest(new TestWebSocketHandler());
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
这是类(TestWebSocketHandler):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.Web.WebSockets;
namespace WebSockets
{
public class TestWebSocketHandler : WebSocketHandler
{
private static WebSocketCollection clients = new WebSocketCollection();
private string name;
public override void OnOpen()
{
this.name = this.WebSocketContext.QueryString["name"];
clients.Add(this);
clients.Broadcast(name + " has connected.");
}
public override void OnMessage(string message)
{
clients.Broadcast(string.Format("{0} said: {1}", name, message));
}
public override void OnClose()
{
clients.Remove(this);
clients.Broadcast(string.Format("{0} has gone away.", name));
}
}
}
答案 0 :(得分:3)
对于此问题的任何影响都没有出现错误,但我发现Chrome无法与我的200响应一起返回的错误。
我只需打开控制面板下的WebSocket协议 - &gt;程序 - &gt;打开或关闭Windows功能 - &gt; IIS - &gt;应用程序开发功能 - &gt; WebSocket协议。
至少我在网站上添加了一个简单的VB.NET WebSocket。
确保您拥有IIS 7+并在4.5 Framework中运行Windows 8并打开上面的WebSocket功能。
答案 1 :(得分:0)
200代码不是错误,它是HTTP OK响应,这是你想要的。使用浏览器检查响应,响应中是否有内容?尝试在ws.onmessage上添加一个断点,看它是否会激活。
答案 2 :(得分:0)
只需检查Web套接字协议表单添加或删除程序
转到控制面板
然后点击添加或删除程序
然后打开或关闭Windows功能
然后 IIS
然后应用程序开发功能
然后选中WebSocket Protocol选项的复选框
我在 Microsoft Windows Server 2012
上测试了它答案 3 :(得分:-3)
我在维基百科中找到了这个:
200 OK 成功HTTP请求的标准响应。实际响应取决于使用的请求方法。在GET请求中,响应将包含与所请求资源相对应的实体。在POST请求中,响应将包含描述或包含操作结果的实体。