Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send binary data with socket.io?

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

There is not much information neither in:

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

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

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

My answer is bellow.

like image 348
chelo_c Avatar asked Dec 03 '15 02:12

chelo_c


People also ask

How does Socket.IO send binary data?

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

How do you emit data into a Socket?

To emit an event from your client, use the emit function on the socket object. To handle these events, use the on function on the socket object on your server. Sent an event from the client!

Is Socket.IO peer to peer?

Socket.IO P2P provides an easy and reliable way to setup a WebRTC connection between peers and communicate using the socket.


1 Answers

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

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

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

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!

like image 50
chelo_c Avatar answered Sep 18 '22 07:09

chelo_c