简体   繁体   中英

Streaming data over WebSocket from JS client to Go server

Background

I intend to stream 100x MB or GB of data to a WebSocket server by JS clients. Like this post:

Streaming data over WebSocket

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:

Streaming data over WebSocket

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM