我按照以下教程在C#中设置了websocket服务器: https://developer.mozilla.org/pt-BR/docs/WebSockets/Writing_WebSocket_server
在此之后,我写了一个部分,它会解码一个正常的消息并在控制台中显示给我。
现在,我希望能够将此消息发送回客户端(具体是ws://echo.websocket.org)
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace sockecho
{
class Program
{
static void Main(string[] args)
{
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
server.Start();
Console.WriteLine("Server has started on 127.0.0.1:80.{0}Waiting for a connection...", Environment.NewLine);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream();
//enter to an infinite cycle to be able to handle every change in stream
while (true)
{
while (!stream.DataAvailable) ;
Byte[] bytes = new Byte[client.Available];
stream.Read(bytes, 0, bytes.Length);
//translate bytes of request to string
String data = Encoding.UTF8.GetString(bytes);
if (new Regex("^GET").IsMatch(data))
{
#region startdata
Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine + "Upgrade: websocket" + Environment.NewLine + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
SHA1.Create().ComputeHash(
Encoding.UTF8.GetBytes(
new Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
)
)
) + Environment.NewLine
+ Environment.NewLine);
stream.Write(response, 0, response.Length);
string responsel = Encoding.UTF8.GetString(response);
Console.WriteLine(responsel);
#endregion
}
else
{
//stream.Write(bytes, 0, bytes.Length);
string decodedmessage = GetMessage(bytes);
Console.WriteLine(decodedmessage);
byte[] toBytes = Encoding.ASCII.GetBytes(decodedmessage);
stream.Write(toBytes, 0, toBytes.Length);
}
}
}
private static string GetMessage (Byte[] bytes)
{
string DETEXT = "";
if (bytes[0] == 129)
{
int position = 0;
int Type = 0;
ulong length = 0;
if (bytes[1] - 128 >= 0 && bytes[1] - 128 <= 125)
{
length = (ulong)bytes[1] - 128;
position = 2;
}
else if (bytes[1] - 128 == 126)
{
Type = 1;
length = (ulong)256 * bytes[2] + bytes[3];
position = 4;
}
else if (bytes[1] - 128 == 127)
{
Type = 2;
for (int i = 0; i < 8; i++)
{
ulong pow = Convert.ToUInt64(Math.Pow(256, (7 - i)));
length = length + bytes[2 + i] * pow;
position = 10;
}
}
else
{
Type = 3;
Console.WriteLine("error 1");
}
if (Type < 3)
{
Byte[] key = new Byte[4] { bytes[position], bytes[position + 1], bytes[position + 2], bytes[position + 3] };
Byte[] decoded = new Byte[bytes.Length - (4 + position)];
Byte[] encoded = new Byte[bytes.Length - (4 + position)];
for (long i = 0; i < bytes.Length - (4 + position); i++) encoded[i] = bytes[i + position + 4];
for (int i = 0; i < encoded.Length; i++) decoded[i] = (Byte)(encoded[i] ^ key[i % 4]);
DETEXT = Encoding.UTF8.GetString(decoded);
}
}
else
{
Console.WriteLine("error 2: " + bytes[0].ToString());
}
return DETEXT;
}
}
}