TCP客户端模拟链接websocket服务端
因一些特殊原因研究了下TCP模拟链接websocket。原理上可以连接但具体怎么连接怎么操作就不知道了,需要研究下,以下是个人研究的方案。
用线上和本地地址来做例子:
线上wss地址:wss://server.cs.com/cs/vido/1
本地地址ws://127.0.0.1/cs/vido/1
如果用本地地址来模拟websocket,需要遵从websocket协议。WebSocket协议需要通过已建立的TCP连接来传输数据。具体实现上是通过http协议建立通道,然后在此基础上用真正的WebSocket协议进行通信,所以WebSocket协议和http协议是有一定的交叉关系的。
在tcp与websocket消息交互之前需要先和websocket建立握手关系:
GET /cs/vido HTTP/1.1
Host: server.cs.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Version: 13
Get输出的是ws后面的子路径地址
Host输出的是域名地址
upgrade、Connection输出的是要升级到websocket通讯
Sec-WebSocket-Key输出的是Base64 encode 的值,这个是随机生成的。
Sec-WebSocket-Version输出的是websocket的版本,默认必须是13
websocket服务器收到客户端消息后会返回验证消息;
HTTP/1.1 101
Connection: upgrade
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Upgrade: websocket
Sec-WebSocket-Accept: sT7MD7zMs7k3yPTFV6JvttwPwoE=
客户端接收到服务端发送的新的Sec-WebSocket-Accept参数后,使用原来的随机密钥和新的Sec-WebSocket-Accept参数共同生成一个新的Sec-WebSocket-Key参数,用于加密数据传输。
客户端将新的Sec-WebSocket-Key参数发送给服务端,服务端接收到后,使用该参数加密数据传输,收到此消息后是代表已经和websocket建立了联系。
这块要注意下,如果是用本地地址去测试没问题,但用正式地址去测试就会被提示需要ssl加密。
使用ssl加密代码如下:
_tcpClient = new TcpClient(host, port);
_networkStream = _tcpClient.GetStream();
// 创建SslStream
SslStream sslStream = new SslStream(_networkStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
// 验证服务器证书
sslStream.AuthenticateAsClient(host);
// 验证服务器证书的回调函数
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
return false;
}
用了ssl加密和没用ssl加密的获取的消息也是不一样的,如果用了ssl加密后就要用sslStream来接收消息或者发送消息否则会乱码。没有用ssl加密那就用_networkStream来接收或者发送消息。
整体代码如下:
public class WebSocketClient2
{
private TcpClient _tcpClient;
private NetworkStream _networkStream;
public void ConnectToWebSocket(string host, int port, string resource)
{
_tcpClient = new TcpClient(host, port);
_networkStream = _tcpClient.GetStream();
// 创建SslStream
SslStream sslStream = new SslStream(_networkStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
// 验证服务器证书
sslStream.AuthenticateAsClient(host);
// 构建WebSocket握手消息
string key = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
string handshakeMessage = $"GET {resource} HTTP/1.1\r\n" +
$"Host:{host}\r\n" +
"Upgrade: websocket\r\n" +
"Connection: Upgrade\r\n" +
$"Sec-WebSocket-Key: {key}\r\n" +
"Sec-WebSocket-Version: 13\r\n" +
"\r\n";
// 发送握手消息
byte[] handshakeBuffer = Encoding.UTF8.GetBytes(handshakeMessage);
//_networkStream.Write(handshakeBuffer, 0, handshakeBuffer.Length);
sslStream.Write(handshakeBuffer, 0, handshakeBuffer.Length);
// 接收服务器响应并验证
byte[] buffer = new byte[1024];
//int bytesRead = _networkStream.Read(buffer, 0, buffer.Length);
int bytesRead = sslStream.Read(buffer, 0, buffer.Length);
string response = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine(response);
// 验证服务器响应的Sec-WebSocket-Accept
string expectedResponse = Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")));
if (response.Contains($"Sec-WebSocket-Accept: {expectedResponse}"))
{
Console.WriteLine("WebSocket handshake successful.");
}
else
{
Console.WriteLine("WebSocket handshake failed.");
}
byte[] buffer2 = new byte[1024];
int bytesRead2;
while ((bytesRead2 = sslStream.Read(buffer2, 0, buffer2.Length)) > 0)
{
string message = Encoding.UTF8.GetString(buffer2, 0, bytesRead2);
// 解析WebSocket消息
// ...
Console.WriteLine($"Received message: {message}");
}
}
// 验证服务器证书的回调函数
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
return false;
}
}
//代码实现调用
WebSocketClient2 client = new WebSocketClient2();
client.ConnectToWebSocket("server.cs.com.cn", 443, "/cs/vido/1");//测试环境ws端口号默认是80,wss默认端口号是443