gpt4 book ai didi

c# - javascript和C#服务器通信

转载 作者:行者123 更新时间:2023-11-30 18:40:05 25 4
gpt4 key购买 nike

我用 C#(客户端-服务器)开发了一个应用程序,使用套接字在客户端和服务器之间进行通信。现在我想使用 javascript(基于 development guide about javascript)为 android 创建相同的应用程序,但要保留用 C# 制作的服务器(我可以通过通信更改部分,但不多)。这是代码的样子:

服务器端:

connection.Send(Encoding.ASCII.GetBytes("HTTP/1.1 101 Switching Protocols\r\nUpgrade:  WebSocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + aux));
connection.Send(Encoding.ASCII.GetBytes("\r\n\r\n"));

aux 在哪里:

public static String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
{
const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
String secWebSocketAccept = String.Empty;

// 1. Combine the request Sec-WebSocket-Key with magic key.
String ret = secWebSocketKey + MagicKEY;

Console.WriteLine("- " + ret + " -");

// 2. Compute the SHA1 hash
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));

// 3. Base64 encode the hash
secWebSocketAccept = Convert.ToBase64String(sha1Hash);

return secWebSocketAccept;
}

我使用 websockets 使用 javascript 创建了一个连接到 c sharp 服务器的客户端。问题是握手后我的连接关闭了,我不知道为什么。我得到的唯一错误是:“无法识别的帧操作码:7”。我使用谷歌浏览器 16.0.912.75。

一步一步:

  1. WebSocket 可用
  2. 服务器收到握手请求
  3. 生成Sec-WebSocket-Accept并发送给浏览器
  4. 浏览器识别 key 并执行.onopen方法
  5. 之后我得到错误

    var ws;
    $(document).ready(function () {

    // test if the browser supports web sockets
    if ("WebSocket" in window) {
    debug("Browser supports web sockets!", 'success');
    connect($('#host').val());
    $('#console_send').removeAttr('disabled');
    } else {
    debug("Browser does not support web sockets", 'error');
    };

    // function to send data on the web socket
    function ws_send(str) {
    try {
    ws.send(str);
    } catch (err) {
    debug(err, 'error');
    }
    }

    // connect to the specified host
    function connect(host) {

    debug("Connecting to " + host + " ...");
    try {
    ws = new WebSocket(host); // create the web socket
    } catch (err) {
    debug(err, 'error');
    }
    $('#host_connect').attr('disabled', true); // disable the 'reconnect' button

    ws.onopen = function () {
    debug("connected... ", 'success'); // we are in! Big Grin | :-D
    };

    ws.onmessage = function (evt) {
    debug(evt.data, 'response'); // we got some data - show it omg!!
    };

    ws.onclose = function () {
    debug("Socket closed!", 'error'); // the socket was closed (this could be an error or simply that there is no server)
    $('#host_connect').attr('disabled', false); // re-enable the 'reconnect button
    };
    };

    // function to display stuff, the second parameter is the class of the <p> (used for styling)
    function debug(msg, type) {
    $("#console").append('<p class="' + (type || '') + '">' + msg + '</p>');
    };

    // the user clicked to 'reconnect' button
    $('#host_connect').click(function () {
    debug("\n");
    connect($('#host').val());
    });

    // the user clicked the send button
    $('#console_send').click(function () {
    ws_send($('#console_input').val());
    });

    $('#console_input').keyup(function (e) {
    if(e.keyCode == 13) // enter is pressed
    ws_send($('#console_input').val());
    });

    });

如果您需要更多信息,请回复。我现在在谷歌上搜索这个操作码:7 问题大约 4 小时。

最佳答案

您还应该看看 SuperWebSocket这是一个开源的 .NET WebSocket 服务器。查看那里的代码应该可以帮助您解决问题。

关于c# - javascript和C#服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8826391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com