使用javascript从浏览器连接到TCP Socket [英] Connecting to TCP Socket from browser using javascript

查看:211
本文介绍了使用javascript从浏览器连接到TCP Socket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 vb.net 应用程序,它打开一个套接字并监听它.

I have a vb.net application that opens a socket and listens on it.

我需要使用在浏览器上运行的 javascript 通过此套接字与该应用程序进行通信.那就是我需要在这个套接字上发送一些数据,以便在这个套接字上侦听的应用程序可以获取该数据,使用一些远程调用做一些事情并获取更多数据并将其放回我的 javascript 需要的套接字上在浏览器中阅读并打印.

I need to communicate via this socket to that application using a javascript running on a browser. That is i need to send some data on this socket so that the app which is listening on this socket can take that data, do some stuff using some remote calls and get some more data and put it back on the socket that my javascript needs to read and print it in the browser.

我尝试过 socket.io、websockify 但没有一个被证明是有用的.

Ive tried, socket.io, websockify but none have proved to be useful.

因此问题是,我正在尝试的可能吗?有没有办法让在浏览器中运行的 javascript 可以连接到 tcp 套接字并发送一些数据并监听它以获取套接字上的更多数据响应并将其打印到浏览器.

Hence the question, is what i am trying even possible? Is there a way that a javascript running in a browser can connect to a tcp socket and send some data and listen on it for some more data response on the socket and print it to the browser.

如果可以的话,有人可以为我指出正确的方向,以帮助我建立目标.

If this is possible can some one point me in the right direction as to which would help me establish the goal.

推荐答案

至于你的问题,目前你将不得不依赖 XHR 或 websockets.

As for your problem, currently you will have to depend on XHR or websockets for this.

目前还没有流行的浏览器为 javascript 实现任何这样的原始套接字 api,允许您创建和访问原始套接字,但在 JavaScript 中实现原始套接字 api 的草案正在进行中.看看这些链接:
http://www.w3.org/TR/raw-sockets/
https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket

Currently no popular browser has implemented any such raw sockets api for javascript that lets you create and access raw sockets, but a draft for the implementation of raw sockets api in JavaScript is under-way. Have a look at these links:
http://www.w3.org/TR/raw-sockets/
https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket

Chrome 现在在其实验性"API 中支持原始 TCP 和 UDP 套接字.这些功能仅适用于扩展程序,并且虽然已记录在案,但目前处于隐藏状态.话虽如此,一些开发人员已经在使用它创建有趣的项目,例如这个 IRC 客户端.

Chrome now has support for raw TCP and UDP sockets in its ‘experimental’ APIs. These features are only available for extensions and, although documented, are hidden for the moment. Having said that, some developers are already creating interesting projects using it, such as this IRC client.

要访问此 API,您需要在扩展程序的清单中启用实验标志.使用套接字非常简单,例如:

To access this API, you’ll need to enable the experimental flag in your extension’s manifest. Using sockets is pretty straightforward, for example:

chrome.experimental.socket.create('tcp', '127.0.0.1', 8080, function(socketInfo) {
  chrome.experimental.socket.connect(socketInfo.socketId, function (result) {
        chrome.experimental.socket.write(socketInfo.socketId, "Hello, world!");         
    });
});

这篇关于使用javascript从浏览器连接到TCP Socket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆