简体   繁体   English

html javascript连接到原始套接字

[英]html javascript connect to raw socket

I have ac# tcp server, I want to connect to the server via a html client page.我有 ac# tcp 服务器,我想通过 html 客户端页面连接到服务器。

The problem : There is no simple way to create TCP sockets in Javascript on a browser side.问题:没有简单的方法可以在浏览器端用 Javascript 创建 TCP 套接字。 Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets.尽管像 Websockets 这样的解决方案允许创建类似于套接字的东西,但您只能使用它们连接到支持 Websockets 的服务器。 Not to any random servers that know nothing about HTTP.不是任何对 HTTP 一无所知的随机服务器。

so is there a solution to connect to my srver.那么是否有连接到我的服务器的解决方案。

No. There just isn't.不,只是没有。 The browser is a tightly locked down environment.浏览器是一个严密锁定的环境。 The only socket connection that you can open from JavaScript is WebSocket.您可以从 JavaScript 打开的唯一套接字连接是 WebSocket。 Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.由于它是您的服务器,因此添加 WebSocket 支持应该不会太复杂,并且有可用于 C# 的 WebSocket 库。

Maybe someone else will have an idea for you, but...也许其他人会对你有想法,但是......

The best solution I can think of is for your server to support websockets.我能想到的最佳解决方案是让您的服务器支持 websockets。

The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.您描述的情况 - 以及通过代理和路由器的流量的连接问题 - 是首先引入 Websockets 的原因之一。

Bare in mind that Websockets can send and receive binary data.请记住,Websockets 可以发送和接收二进制数据。 It's just that javascript make it more comfortable to write text based messages.只是 javascript 使编写基于文本的消息更加舒适。

Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through.此外,许多 NAT 路由器、代理和防火墙将阻止原始 TCP/IP 通信,同时允许 Http 通信通过。 This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.这就是为什么在实现 Websocket 协议时您有更好的机会建立和保持连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM