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

How to send binary data with socket.io?(如何使用 socket.io 发送二进制数据?)
本文介绍了如何使用 socket.io 发送二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

所以我在 node.js(Js 客户端和 Android 客户端)中使用 socket.io 发送二进制数据时遇到了问题.

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

没有太多信息:

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

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

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

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

他们给出的唯一代码如下:

the only code they give is the following:

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

我的答案如下.

推荐答案

终于可以在 JS 和 Android ( Java ) 上运行了,所以我决定和大家分享一下.

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

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

Let's start with the Server Code: (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);

让我们跳到 Javascript 客户端

Lets jump on to the Javascript client

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

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

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

    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!

这篇关于如何使用 socket.io 发送二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Fastest way to generate all binary strings of size n into a boolean array?(将所有大小为 n 的二进制字符串生成为布尔数组的最快方法?)
Convert integer to zero-padded binary string(将整数转换为零填充的二进制字符串)
How Can I Convert Very Large Decimal Numbers to Binary In Java(如何在 Java 中将非常大的十进制数转换为二进制数)
Check if only one single bit is set within an integer (whatever its position)(检查整数中是否只设置了一个位(无论其位置如何))
Embed a Executable Binary in a shell script(在 shell 脚本中嵌入可执行二进制文件)
why does quot;STRINGquot;.getBytes() work different according to the Operation System(为什么“STRING.getBytes() 的工作方式因操作系统而异)