我正在尝试实现一个C#Web套接字服务器,但它给了我一些麻烦.我正在运行一个Web服务器(ASP.NET)来托管带有javascript的页面,而Web套接字服务器则是作为C#控制台应用程序实现的.
我能够检测来自客户端的连接尝试(运行javascript的chrome)以及从客户端检索握手.但是客户端似乎不接受我发回的握手(onopenWeb套接字上的函数永远不会被调用).
我一直在阅读The Web Socket协议,我看不出我做错了什么.下面是一些服务器代码:
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8181);
listener.Bind(ep);
listener.Listen(100);
Console.WriteLine("Wainting for connection...");
Socket socketForClient = listener.Accept();
if (socketForClient.Connected)
{
Console.WriteLine("Client connected");
NetworkStream networkStream = new NetworkStream(socketForClient);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
//read handshake from client:
Console.WriteLine("HANDSHAKING...");
char[] shake = new char[255];
streamReader.Read(shake, 0, 255);
string handshake =
"HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"WebSocket-Origin: http://localhost:8080\r\n" +
"WebSocket-Location: ws://localhost:8181\r\n" +
"\r\n";
streamWriter.Write(handshake);
streamWriter.Flush();
Run Code Online (Sandbox Code Playgroud)
我正在我的localhost上运行端口8080上的Web服务器和端口8181上的Web套接字服务器.
我尝试用不同的编码(ASCII,字节和十六进制)发送握手,但这似乎没有什么区别.连接永远不会完全建立.javascript看起来像这样:
var ws;
var host = 'ws://localhost:8181';
debug("Connecting to " + host + " ...");
try {
ws = new WebSocket(host);
} catch (err) {
debug(err, 'error');
}
ws.onopen = function () {
debug("connected...", 'success');
};
ws.onclose = function () {
debug("Socket closed!", 'error');
};
ws.onmessage = function (evt) {
debug('response: ' + evt, 'response');
};
Run Code Online (Sandbox Code Playgroud)
我猜测错误在于C#服务器,因为chrome正在按原样发送信息,但正如所说的那样,onopen函数永远不会被调用.
所以我的问题简而言之: 你们有没有成就过 - 如果是的话,你是怎么做到的?原因:你看到代码中有任何明显的错误(希望不要问太多)
Dar*_*rov 47
可能这是一个编码问题.这是我写的一个有效的C#服务器:
class Program
{
static void Main(string[] args)
{
var listener = new TcpListener(IPAddress.Loopback, 8181);
listener.Start();
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
{
writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake");
writer.WriteLine("Upgrade: WebSocket");
writer.WriteLine("Connection: Upgrade");
writer.WriteLine("WebSocket-Origin: http://localhost:8080");
writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
writer.WriteLine("");
}
listener.Stop();
}
}
Run Code Online (Sandbox Code Playgroud)
并且相应的客户端托管在localhost:8080:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var socket = new WebSocket('ws://localhost:8181/websession');
socket.onopen = function() {
alert('handshake successfully established. May send data now...');
};
socket.onclose = function() {
alert('connection closed');
};
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
此示例仅建立握手.您需要调整服务器,以便在建立握手后继续接受数据.
Ker*_*ang 24
请使用UTF8编码发送短信.
有一个由C#实现的开源websocket服务器,你可以直接使用它.
http://superwebsocket.codeplex.com/
这是我的开源项目!
| 归档时间: |
|
| 查看次数: |
69817 次 |
| 最近记录: |