英文:
Streaming data over WebSocket from JS client to Go server
问题
背景
我打算通过JS客户端将100x MB
或GB
的数据流式传输到一个WebSocket服务器。就像这篇帖子中所描述的那样:
https://stackoverflow.com/q/29000647/3405291
但是我的服务器是用Go而不是JS实现的。我的意思是有一个由https://github.com/gorilla/websocket 实现的Go WebSocket服务器。
有一种建议是使用BinaryJS:
https://stackoverflow.com/questions/29000647/streaming-data-over-websocket#comment46249091_29000647
问题
BinaryJS:服务器和客户端都是JS
服务器
var BinaryServer = require('../../').BinaryServer;
// Start Binary.js server
var server = BinaryServer({port: 9000});
客户端
<script src="http://cdn.binaryjs.com/0/binary.js"></script>
<script>
// Connect to Binary.js server
var client = new BinaryClient('ws://localhost:9000');
问题
是否可以在Go服务器中使用BinaryJS?是否有任何等效的Go包?
英文:
Background
I intend to stream 100x MB
or GB
of data to a WebSocket server by JS clients. Like this post:
https://stackoverflow.com/q/29000647/3405291
But my server is in Go rather than JS. I mean there is a Go WebSocket server implemented by https://github.com/gorilla/websocket
One option suggested is to use BinaryJS:
https://stackoverflow.com/questions/29000647/streaming-data-over-websocket#comment46249091_29000647
Problem
BinaryJS: both server and client are JS
Server
var BinaryServer = require('../../').BinaryServer;
// Start Binary.js server
var server = BinaryServer({port: 9000});
Client
<script src="http://cdn.binaryjs.com/0/binary.js"></script>
<script>
// Connect to Binary.js server
var client = new BinaryClient('ws://localhost:9000');
Question
Is it possible to use BinaryJS along with a Go server? Is any equivalent Go package?
答案1
得分: 0
对于大小超过200MB的Float32Array
类型的数据从JS发送到Go WebSocket服务器,测试表明根本不需要流式传输。
只需确保在ws.send(positions);
之前使用ws.binaryType = 'arraybuffer';
。
var positions = attrPos.array;
function connect() {
return new Promise(function(resolve, reject) {
var ws = new WebSocket('ws://127.0.0.1:8081/echo');
ws.onopen = function() {
resolve(ws);
};
ws.onerror = function(err) {
reject(err);
};
ws.onclose = function(evt) {
console.log("CLOSE SOCKET", new Date().toLocaleString());
};
ws.onmessage = function(evt) {
console.log("RESPONSE SOCKET: " + "RECEIVED" /* evt.data */, new Date().toLocaleString());
};
});
}
connect().then(function(ws) {
// onopen
console.log("OPENED SOCKET", new Date().toLocaleString());
console.log("SEND: START", new Date().toLocaleString());
ws.binaryType = 'arraybuffer'; // ** Critical statement
ws.send(positions);
ws.close();
}).catch(function(err) {
// onerror
console.log("ERROR: " + evt.data, new Date().toLocaleString());
});
英文:
For data of Float32Array
type above 200 MB
in size to be sent from JS to a Go WebSocket server, there is no need for streaming at all, tests show.
Just make sure ws.binaryType = 'arraybuffer';
is used before ws.send(positions);
.
var positions = attrPos.array;
function connect() {
return new Promise(function(resolve, reject) {
var ws = new WebSocket('ws://127.0.0.1:8081/echo');
ws.onopen = function() {
resolve(ws);
};
ws.onerror = function(err) {
reject(err);
};
ws.onclose = function(evt) {
console.log("CLOSE SOCKET", new Date().toLocaleString());
};
ws.onmessage = function(evt) {
console.log("RESPONSE SOCKET: " + "RECEIVED" /* evt.data */, new Date().toLocaleString());
};
});
}
connect().then(function(ws) {
// onopen
console.log("OPENED SOCKET", new Date().toLocaleString());
console.log("SEND: START", new Date().toLocaleString());
ws.binaryType = 'arraybuffer'; // ** Critical statement
ws.send(positions);
ws.close();
}).catch(function(err) {
// onerror
console.log("ERROR: " + evt.data, new Date().toLocaleString());
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论