使用 javascript 通过 websockets 传输文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11390021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
transferring files with javascript through websockets
提问by DasBoot
hello i am trying to transfer files. I have some programs converting files to binary and transferring them over a network with c++. I was wondering if i would be able to transfer files with javascripts and websockets? any examples on how to integrate my c++ program into javascript would be appreciated. thanks.
您好,我正在尝试传输文件。我有一些程序将文件转换为二进制文件并使用 C++ 通过网络传输它们。我想知道我是否可以使用 javascripts 和 websockets 传输文件?任何关于如何将我的 C++ 程序集成到 javascript 的示例将不胜感激。谢谢。
回答by kanaka
Javascript has two new binary types: typed arrays (arraybuffers) and Blobs (basically files).
Javascript 有两种新的二进制类型:类型数组(arraybuffers)和 Blob(基本上是文件)。
WebSockets support sending and receiving typed arrays and blobs.
WebSockets 支持发送和接收类型化数组和 blob。
To transfer data between two browsers using WebSockets you will need a server for them both to connect to (browser WebSocket support is client only at this point).
要使用 WebSockets 在两个浏览器之间传输数据,您需要一个服务器来连接它们(此时浏览器 WebSocket 支持仅是客户端)。
If you have an existing server in C++ that handles file transport then you should be able to add WebSocket server support to it fairly easily. You can find WebSocket client and server implementations on this page: http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations
如果你有一个现有的 C++ 服务器来处理文件传输,那么你应该能够很容易地向它添加 WebSocket 服务器支持。您可以在此页面上找到 WebSocket 客户端和服务器实现:http: //en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations
In JavaScript to establish a connection to a WebSocket server you do something like this:
在 JavaScript 中,要建立到 WebSocket 服务器的连接,您可以执行以下操作:
ws = new WebSocket("ws://100.101.102.103");
The send() method support normal strings, typed arrays or blobs. Sending typed arrays and blobs will result in the frame(s) received by the server as binary frames (opcode = 2).
send() 方法支持普通字符串、类型化数组或 blob。发送类型化数组和 blob 将导致服务器作为二进制帧(操作码 = 2)接收的帧。
ws.send(myTypedArray);
To receive messages you register a message handler:
要接收消息,请注册消息处理程序:
ws.onmessage = function (evt) {
console.log("Got ws message: " + evt.data);
};
If the server sends a binary frame/message then the onmessage data property of the event will contain either a typed array or blob depending on the setting of the binaryType attribute. You can change the type of the binary data that is received like this:
如果服务器发送二进制帧/消息,则事件的 onmessage 数据属性将包含类型化数组或 blob,具体取决于 binaryType 属性的设置。您可以像这样更改接收到的二进制数据的类型:
ws.binaryType = "blob"; // or "arraybuffer"
回答by duskwuff -inactive-
What you're trying to do isn't possible. WebSocket can only operate in a client mode; it cannot accept connections from another WebSocket client (such as another browser).
你试图做的事情是不可能的。WebSocket 只能在客户端模式下运行;它不能接受来自另一个 WebSocket 客户端(例如另一个浏览器)的连接。
It is possible to hook WebSocket clients to one another through a server, but at that point it's no longer really peer-to-peer, so I'm not sure if that's really useful or interesting anymore.
可以通过服务器将 WebSocket 客户端相互挂钩,但那时它不再是真正的点对点,所以我不确定这是否真的有用或有趣了。