首页 > 解决方案 > WebSocketSharp 异常:System.PlatformNotSupportedException:“此平台不支持操作。”

问题描述

我正在使用通过安全 websocket 与 node.js 服务器通信的 C# 应用程序。还有一个 javascript 客户端连接到节点服务器上托管的同一个 websocket。javascript 客户端连接成功连接到节点服务器。但是 c# 应用程序会抛出标题中描述的错误。

我使用了 chrome 的 SimpleWebSocketClient 扩展来连接到我的套接字。这工作正常。我还为 C# 应用程序尝试了 websocket4net。这也不起作用。我在我的 c# 应用程序中使用了 wss://echo.websocket.org websocket,它运行良好。但是我必须设置_socket.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.tls12;

节点服务器

const app = require('express')()
const fs      = require("fs");        // file system core module
var https   = require("https").createServer({
    cert: fs.readFileSync(__dirname + "/certs/cert.pem"),
    key:  fs.readFileSync(__dirname + "/certs/key.pem")
}, app)
const express = require("express");   // web framework external module

// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
app.use(express.static(__dirname + "/static/"));

const ws = require('ws')
const wss = new ws.Server({
    server: https
});

wss.on('connection', function connection(ws) {
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
        ws.send('reply from server : ' + message)
    });

    ws.send('something');
});

// Listen on port 8080
https.listen(8080, () => {
    console.log('listening on https://localhost:8080')
})

这是 C# 代码

_socket = new WebSocket("wss://127.0.0.1:8080");

            _id = Guid.NewGuid().ToString();

            _socket.OnOpen += (sender, e) =>
            {
                Console.WriteLine("WebSocket Connection is now Open with ID: " + _id);


            };

            _socket.OnMessage += (sender, e) =>
            {
                Console.WriteLine("On Message");
                Console.WriteLine(e.Data);
            };

            _socket.Connect();

我很困惑为什么我的 C# 应用程序不起作用。它应该能够建立连接,就像wss://echo.websocket.org

标签: c#node.jswebsocket

解决方案


推荐阅读