go-socket.io icon indicating copy to clipboard operation
go-socket.io copied to clipboard

How to send binary data to js client?

Open Charlotte3240 opened this issue 2 years ago • 3 comments

server code

s.Server.OnEvent("/", "uploadPcm", func(c socketio.Conn, msg interface{}, buffer parser.Buffer) {
		log.Println("uploadPcm:", msg)
		log.Println("buffer:", len(buffer.Data))
		c.Emit("receivePcm", `{"sequence":`+strconv.Itoa(1)+`}`, buffer.Data)
	})

js recevie an base64 formater data ?

what happen ?

js client error

socket.io1.4.8.min.js:3 Uncaught TypeError: Cannot read properties of undefined (reading 'base64')

when I send the parser.Buffer

server code

		c.Emit("receivePcm", `{"sequence":`+strconv.Itoa(1)+`}`, parser.Buffer{Data: buffer.Data})

Charlotte3240 avatar Mar 03 '22 08:03 Charlotte3240

Heyy I have the same issue... I'm trying to send a packet of type map[interface]interface{} and I get the same error.

I tried debugging the socket.io-client package in node_modules.. The error arises in the following line...

/node_modules/socket.io-parser/index.js | Socket.io client version - 1.3

/**
 * Decodes an ecoded packet string into packet JSON.
 *
 * @param {String} obj - encoded packet
 * @return {Object} packet
 * @api public
 */

Decoder.prototype.add = function(obj) {
  var packet;
  if ('string' == typeof obj) {
    packet = decodeString(obj);
    if (exports.BINARY_EVENT == packet.type || exports.BINARY_ACK == packet.type) { // binary packet's json
      this.reconstructor = new BinaryReconstructor(packet);

      // no attachments, labeled binary but no binary data to follow
      if (this.reconstructor.reconPack.attachments === 0) {
        this.emit('decoded', packet);
      }
    } else { // non-binary full packet
      this.emit('decoded', packet);
    }
  }
  else if (isBuf(obj) || obj.base64) { // raw binary data <<<<<-------------- Error Here ------------------------<<<<<
    if (!this.reconstructor) {
      throw new Error('got binary data when not reconstructing a packet');
    } else {
      packet = this.reconstructor.takeBinaryData(obj);
      if (packet) { // received final buffer
        this.reconstructor = null;
        this.emit('decoded', packet);
      }
    }
  }
  else {
    throw new Error('Unknown type: ' + obj);
  }
};

After further debugging I see that the argument obj itself is undefined which means the client did not receive any data packet from the server despite emitting one in the server side code.

I believe the bug is in (s socketio.Conn) func Emit(), (server* socketio.Server) func BroadcastToRoom() and as well in (server* socketio.Server) func BroadcastToNamespace() where the arguments we pass is not being sent to the client, especially those of type map[interface]interface{}

That's all I was able to figure out I don't have enough knowledge about this package to contribute a fix :(

@Charlotte3240 please let me know if you have found a workaround for this

HardCodeProgrammer avatar Aug 06 '22 07:08 HardCodeProgrammer

@HardCodeProgrammer Please I am sending a binary packet using Websockets like this

socket.send('452-["binary",{"_placeholder":true,"num":0},{"_placeholder":true,"num":1}]');
socket.send(Uint8Array.from([1, 2, 3]));
socket.send(Uint8Array.from([4, 5, 6]));

How will the handler function be in golang?

Ekenzy-101 avatar Aug 18 '22 18:08 Ekenzy-101

Any update on this point. When I am emitting byte array on socketio Server. Client (Postman) is receiving base64 string. Is it expected?

Sahil624 avatar Aug 22 '23 11:08 Sahil624