日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

websocket服务器响应头,从服务器发送响应握手后,websocket.onopen不会触发

發布時間:2024/1/23 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 websocket服务器响应头,从服务器发送响应握手后,websocket.onopen不会触发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面我已詳細分享了我的代碼。我閱讀文檔和關于握手的一切。我遵循了文檔中提供的所有步驟以及互聯網上的眾多示例,但仍然存在此問題。奇怪的事情id websocket.onclsose()在我關閉服務器時被觸發。從服務器發送響應握手后,websocket.onopen不會觸發

// Simple Websocket server

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net.Sockets;

using System.Net;

using System.IO;

using System.Security.Cryptography;

using System.Threading;

namespace WebSocketServer

{

class Program

{

//port

private static int _port = 8181;

static void Main(string[] args)

{

TcpListener t = new TcpListener(IPAddress.Loopback, _port);

t.Start();

Console.WriteLine("Server is started and waiting for client\n\n");

byte[] buff = new byte[255];

NetworkStream stream;

TcpClient client;

while(true)

{

client = t.AcceptTcpClient();

if (!client.Connected)

return;

// I need form a proper mechanism to get all the data out of network stream.

// If I wait too long client get disconnected and we dont get stream and if

// if we dont wait at all then data doesnt reach server port and hence cant

// read the handshake.

stream = client.GetStream();

while ((stream.Read(buff, 0, buff.Length)) != 0)

{

break;

}

if (0 != buff.Length)

break;

}

StreamWriter writer = new StreamWriter(stream);

writer.AutoFlush = true;

//while (stream.DataAvailable)

//stream.Read(buff, 0, buff.Length);

Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(buff));

string clientHandshake = System.Text.ASCIIEncoding.ASCII.GetString(buff);

char[] separators = new char[1];

separators[0] = '\n';

string[] temp = clientHandshake.Split(separators, 100);

string keyword = "Sec-WebSocket-Key";

string key = "";

foreach (string s in temp)

{

if (s.Contains(keyword))

{

string keyTemp= s.Substring(keyword.Length + 2);

key = keyTemp.Remove(keyTemp.Length - 1);

break;

}

}

string responseKey = GetServerResponseKey(key);

// Send Server handshake

string handshake =

"HTTP/1.1 101 Switching Protocols\r\n" +

"Upgrade: websocket\r\n" +

"Connection: Upgrade\r\n" +

"Sec-WebSocket-Accept: " + responseKey + "\r\n";

writer.Write(handshake);

writer.Flush();

Console.WriteLine(handshake);

while ((stream.Read(buff, 0, buff.Length)) != 0)

{

break;

}

Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(buff));

// Keep Server alive

while (true)

{ }

}

//Helper method to convert string into Byte[]

private static byte[] GetByteArray(string str)

{

UTF8Encoding encoding = new UTF8Encoding();

return encoding.GetBytes(str);

}

//This method is requuired because it combines key(got it from client)

//with GUID. Then takes SHA1 hash of that string and then encode to base64.

//This is all required because Handshake mechanism can be done by only this

//way according to Websocket Protocol(http://datatracker.ietf.org/doc/rfc6455/)

private static string GetServerResponseKey(string key)

{

Console.WriteLine("original key = " + key);

string keyForHash = String.Concat(key, Guid.NewGuid());

Console.WriteLine("text version of server response key = " + keyForHash);

UTF8Encoding encoding = new UTF8Encoding();

byte[] temp = encoding.GetBytes(keyForHash);

SHA1 hashProvider = new SHA1CryptoServiceProvider();

byte[] keyForBase64 = hashProvider.ComputeHash(temp);

return Convert.ToBase64String(keyForBase64);

}

}

}

// Simple WebSocket Client

var ws;

function btnConnectSend_onclick() {

if ("WebSocket" in window) {

ws = new WebSocket("ws://localhost:8181");

ws.onopen = function() {

alert("Connection Open");

ws.send("Hello Server");

};

ws.onmessage = function(evt) {

form1.txtMessage.value = evt.data;

alert("Server says:" + evt.data);

};

ws.onclose = function() {

alert("Socket Closed!!!");

};

ws.onerror = function() {

alert("WTF!");

};

}

}

function btnClose_onclick() {

ws.close();

};

總結

以上是生活随笔為你收集整理的websocket服务器响应头,从服务器发送响应握手后,websocket.onopen不会触发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。