从JS客户端到Go服务器通过WebSocket传输数据的流程

huangapple go评论86阅读模式
英文:

Streaming data over WebSocket from JS client to Go server

问题

背景

我打算通过JS客户端将100x MBGB的数据流式传输到一个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});

https://github.com/binaryjs/binaryjs/blob/79f51d6431e32226ab16e1b17bf7048e9a7e8cd9/examples/helloworld/server.js#L5

客户端

<script src="http://cdn.binaryjs.com/0/binary.js"></script>
  <script>
    // Connect to Binary.js server
    var client = new BinaryClient('ws://localhost:9000');

https://github.com/binaryjs/binaryjs/blob/79f51d6431e32226ab16e1b17bf7048e9a7e8cd9/examples/helloworld/index.html#L6

问题

是否可以在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});

https://github.com/binaryjs/binaryjs/blob/79f51d6431e32226ab16e1b17bf7048e9a7e8cd9/examples/helloworld/server.js#L5

Client

<script src="http://cdn.binaryjs.com/0/binary.js"></script>
  <script>
    // Connect to Binary.js server
    var client = new BinaryClient('ws://localhost:9000');

https://github.com/binaryjs/binaryjs/blob/79f51d6431e32226ab16e1b17bf7048e9a7e8cd9/examples/helloworld/index.html#L6

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());
					});

huangapple
  • 本文由 发表于 2021年7月26日 20:44:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/68530073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定