java 如何使用socket.io发送二进制数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34056705/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:25:14  来源:igfitidea点击:

How to send binary data with socket.io?

javaandroidnode.jssocket.iobinary

提问by chelo_c

so I have been having real trouble sending binary data with socket.io in node.js (Js client and Android client).

所以我在 node.js(Js 客户端和 Android 客户端)中使用 socket.io 发送二进制数据时遇到了真正的麻烦。

There is not much information neither in:

也没有太多信息:

http://socket.io/blog/introducing-socket-io-1-0/

http://socket.io/blog/introducing-socket-io-1-0/

http://socket.io/get-started/chat/

http://socket.io/get-started/chat/

I need to use socket io to send a binary array, that I create and fill.

我需要使用 socket io 来发送一个我创建和填充的二进制数组。

the only code they give is the following:

他们提供的唯一代码如下:

var socket = new WebSocket('ws://localhost');
socket.binaryType = 'arraybuffer';
socket.send(new ArrayBuffer);

My answer is bellow.

我的回答如下。

回答by chelo_c

Finally I have it working with JS and Android ( Java ), so I decided to share it with you guys.

最后我让它与 JS 和 Android ( Java ) 一起工作,所以我决定与你们分享。

Let's start with the Server Code: (Node js)

让我们从服务器代码开始:(Node js)

var http = require('http');

var app = http.createServer(function ejecute(request, response){});
var io = require('socket.io').listen(app);


io.on('connection', function(socket) {
        socket.on('message', function(data){
            console.log("recieved data:");
            console.log(data);

            var bufArr = new ArrayBuffer(4);
            var bufView = new Uint8Array(bufArr);
            bufView[0]=6;
            bufView[1]=7;
            bufView[2]=8;
            bufView[3]=9;
            socket.emit('message',bufArr);
        });
    });
app.listen(3000);

Lets jump on to the Javascript client

让我们跳到 Javascript 客户端

  var socket = io("http://localhost:3000");
  socket.emit('message', 'hola from js client');

  socket.on('message', function(msg){
    var bufView = new Uint8Array(msg);
    console.log(msg)
  });

And finally, let's show the Android (java) client:

最后,让我们展示一下 Android (java) 客户端:

    final Socket socket = IO.socket("http://localhost:3000",opts);

    socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            socket.emit("message","hello from java");
        }
    });


    socket.on("message", new Emitter.Listener() {

        @Override
        public void call(Object... args) {
            byte[] bytearray = (byte[])args[0]; //received bytes

            for  (byte b : bytearray) {
                System.out.println("byte"+b);
            }
        }

    });

   socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
    @Override
    public void call(Object... args) {}
});

I hope it is useful to you all. Cheers!

我希望它对大家有用。干杯!