所有在线博客都参考了具有旧命名空间的测试版,并使用了nuget软件包。我试图得到一个简单的低级别websockets示例基于现在在野外的什么,它只是不工作。
客户端永远无法建立连接。 chrome调试控制台的错误是: “端口错误:无法建立连接。接收端不存在。”
但是,我知道正在接收请求,因为我将代码放入我的ashx处理程序,以便在各个点向我发送电子邮件,以确认请求已进入并且我的任务正在解雇等。
配置 - 所有最终版本: Windows Server 2012 / IIS 8 / ASP.NET 4.5
我的样本基于: http://www.asp.net/vnext/overview/whitepapers/whats-new#_Toc318097383
处理程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.WebSockets;
using System.Net.WebSockets;
using System.Threading;
using System.Text;
using System.Threading.Tasks;
namespace myWebSocket
{
/// <summary>
/// Summary description for wsHandler
/// </summary>
public class wsHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");
context.AcceptWebSocketRequest(MyWebSocketTask);
}
public async Task MyWebSocketTask(WebSocketContext context)
{
WebSocket socket = context.WebSocket;
while (true)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("email@address.com");
message.Subject = "Handler";
message.From = new System.Net.Mail.MailAddress("michaelo@hostcollective.com");
message.Body = string.Format("Task Launched {0}", socket.State.ToString());
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
smtp.Send(message);
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
// Asynchronously wait for a message to arrive from a client
WebSocketReceiveResult result =
await socket.ReceiveAsync(buffer, CancellationToken.None);
// If the socket is still open, echo the message back to the client
if (socket.State == WebSocketState.Open)
{
string userMessage = Encoding.UTF8.GetString(buffer.Array, 0,
result.Count);
userMessage = "You sent: " + userMessage + " at " +
DateTime.Now.ToLongTimeString();
buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage));
// Asynchronously send a message to the client
await socket.SendAsync(buffer, WebSocketMessageType.Text,
true, CancellationToken.None);
}
else { break; }
}
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
至于客户端,这是最简单的基于websockets.org
的内容<script language="javascript" type="text/javascript">
// var wsUri = "ws://echo.websocket.org/";
var wsUri = "ws://iis8hosting.com/mikey/wshandler.ashx";
var output;
function init() {
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) { onOpen(evt) };
websocket.onclose = function (evt) { onClose(evt) };
websocket.onmessage = function (evt) { onMessage(evt) };
websocket.onerror = function (evt) { onError(evt) };
}
function onOpen(evt) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
}
function onClose(evt) {
writeToScreen("DISCONNECTED");
}
function onMessage(evt) {
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
websocket.close();
}
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
<h2>WebSocket Test</h2><div id="output"></div>
任何人都有任何想法或其他简单的样本,例如这些已经确认可以与最终版本一起使用。感谢您花时间阅读和回复。
答案 0 :(得分:0)
我可以让您的代码在您的代码中进行一些更改。
您可能不需要在您的环境中进行以下所有更改。
在服务器端
在客户端:
我根据以下错误更改了以下代码:)。
此代码也适用于IISExpress 8
var wsUri =“ws://&lt;%:Request.Url.Host%&gt;:&lt;%:Request.Url.Port%&gt;&lt;%:
Response.ApplyAppPathModifier( “〜/ wshandler.ashx”)