是否可以创建一个使用简单套接字而不是 WebSocket 连接到简单 C# 服务器的 javascript 程序。
你能帮我做一个 sample 吗?
最佳答案
没有标准的方法可以通过在网络浏览器中运行的 Javascript 代码建立 TCP 连接。 (请参阅@Johannes Hahn 的回答)
要在您的客户端和服务器之间进行通信,请考虑 Microsoft 的 SignalR图书馆。它旨在允许在浏览器中运行的 Javascript 程序与 C# 服务器通信。 SignalR 将使用 websockets;但是,如果 websocket 无法通过回退到其他传输方式使用,它将继续工作。如果您需要阻止它尝试使用 websockets,您还可以指定传输。
SignalR connection starts as HTTP, and is then promoted to a WebSocket connection if it is available. WebSocket is the ideal transport for SignalR, since it makes the most efficient use of server memory, has the lowest latency, and has the most underlying features (such as full duplex communication between client and server), but it also has the most stringent requirements: WebSocket requires the server to be using Windows Server 2012 or Windows 8, and .NET Framework 4.5. If these requirements are not met, SignalR will attempt to use other transports to make its connections.
此外,请注意,如果您的 Javascript 未在网络浏览器中运行,您可以进行常规网络连接。例如,在 Node.js 上运行的 Javascript 应用程序。
关于javascript - 从 javascript 连接到 C# 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475436/