javascript - 从 javascript 连接到 C# 服务器

标签 javascript sockets websocket

是否可以创建一个使用简单套接字而不是 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/

相关文章:

javascript - 从 JSON 对象 KEYS 创建字符串 - Vanilla JavaScript 或 lodash

java - 将我的客户端 PC 重定向到默认登录页面

android - 如何在 exoplayer 上播放来自 websocket url 的视频流?

html - WebSocket 进展

java - 如果新连接带有相同的客户端 ID,是否可以在服务器端断开与相同客户端 ID 的旧 mqtt 连接?

javascript - 我如何使用 RegExp 用 ${var} 替换模板?

javascript - 如何对一个带有 id 的空 div 求和?

javascript - 为什么我无法访问 LDAP 的所有属性?

c++ - 使用 Boost Asio 套接字为双端队列调用移动构造函数时出现问题

c# - 使用 .net 监控传入/传出的 http 流量