swo*_*ish 97 javascript sockets html5 tcpclient node.js
我有一个vb.net应用程序打开一个套接字并监听它.
我需要使用在浏览器上运行的javascript通过此套接字与该应用程序进行通信.那就是我需要在这个套接字上发送一些数据,以便正在监听这个套接字的应用程序可以获取该数据,使用一些远程调用做一些事情并获取更多数据并将其放回我的javascript需要的套接字上在浏览器中阅读并打印.
我试过,socket.io,websockify但没有一个被证明是有用的.
因此,问题是,我正在尝试甚至可能吗?有没有办法在浏览器中运行的javascript可以连接到tcp套接字并发送一些数据并在其上侦听以在套接字上获得更多数据响应并将其打印到浏览器.
如果可能的话,有人可以指出正确的方向,这将有助于我确定目标.
Rob*_*zvi 48
至于你的问题,目前你将不得不依赖于XHR或websockets.
目前还没有流行的浏览器为javascript实现任何这样的原始套接字api,可以让你创建和访问原始套接字,但是在JavaScript中实现原始套接字api的草案正在进行中.看看这些链接:
http
:
//www.w3.org/TR/raw-sockets/
https://developer.mozilla.org/en-US/docs/Web/API/TCPSocket
Chrome现在支持其"实验"API中的原始TCP和UDP套接字.这些功能仅适用于扩展,虽然有记录,但暂时隐藏.话虽如此,一些开发人员已经在使用它创建有趣的项目,例如这个IRC客户端.
要访问此API,您需要在扩展程序的清单中启用实验标记.使用套接字非常简单,例如:
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!");
});
});
Run Code Online (Sandbox Code Playgroud)
Dar*_*ren 29
你可以使用HTML5 Web Sockets
:
var connection = new WebSocket('ws://IPAddress:Port');
connection.onopen = function () {
connection.send('Ping'); // Send the message 'Ping' to the server
};
Run Code Online (Sandbox Code Playgroud)
http://www.html5rocks.com/en/tutorials/websockets/basics/
您的服务器也必须使用诸如pywebsocket之类的WebSocket服务器进行监听,或者您可以按照Mozilla所述编写自己的服务器
额外:
更新:从2016年1月的W3C草案:
这可以通过导航器界面实现,如下所示:
http://raw-sockets.sysapps.org/#interface-tcpsocket
https://www.w3.org/TR/tcp-udp-sockets/
navigator.tcpPermission.requestPermission({remoteAddress:"127.0.0.1", remotePort:6789}).then(
() => {
// Permission was granted
// Create a new TCP client socket and connect to remote host
var mySocket = new TCPSocket("127.0.0.1", 6789);
// Send data to server
mySocket.writeable.write("Hello World").then(
() => {
// Data sent sucessfully, wait for response
console.log("Data has been sent to server");
mySocket.readable.getReader().read().then(
({ value, done }) => {
if (!done) {
// Response received, log it:
console.log("Data received from server:" + value);
}
// Close the TCP connection
mySocket.close();
}
);
},
e => console.error("Sending error: ", e)
);
}
);
Run Code Online (Sandbox Code Playgroud)
ws2s项目旨在将套接字引入浏览器端 js。它是一个将 websocket 转换为 socket 的 websocket 服务器。
ws2s示意图
代码示例:
var socket = new WS2S("wss://ws2s.feling.io/").newSocket()
socket.onReady = () => {
socket.connect("feling.io", 80)
socket.send("GET / HTTP/1.1\r\nHost: feling.io\r\nConnection: close\r\n\r\n")
}
socket.onRecv = (data) => {
console.log('onRecv', data)
}
Run Code Online (Sandbox Code Playgroud)
见jsocket.我自己没用过.自上次更新至今已超过3年(截至2014年6月26日).
*使用闪光:(
从文档:
<script type='text/javascript'>
// Host we are connecting to
var host = 'localhost';
// Port we are connecting on
var port = 3000;
var socket = new jSocket();
// When the socket is added the to document
socket.onReady = function(){
socket.connect(host, port);
}
// Connection attempt finished
socket.onConnect = function(success, msg){
if(success){
// Send something to the socket
socket.write('Hello world');
}else{
alert('Connection to the server could not be estabilished: ' + msg);
}
}
socket.onData = function(data){
alert('Received from socket: '+data);
}
// Setup our socket in the div with the id="socket"
socket.setup('mySocket');
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
199823 次 |
最近记录: |