nhn / socket.io-client-unity3d

socket.io-Client for Unity3D, which is compatible with socket.io v1.x
Other
167 stars 42 forks source link

[Bug]When the client emit the message included symbol '-', the server can't receive this message #8

Closed gzlock closed 7 years ago

gzlock commented 7 years ago

env: unity3d: 5.6.2 f1 personal socket.io-client-unity3d: Latest version

Repeat the test two times, make sure: when the message include '-', server can't receive this message

ps: I did not test other symbols

gzlock commented 7 years ago

use node.js socket.io-client emit the message include "-" , server can receive this message

gzlock commented 7 years ago

@ppz0th @smallmiro I tested again, not the symbol make this bug. It's unknown bug!

c# login.cs

Socket.io server just can receive numberic string from socket.io-client-unity3d client. I don't know why!!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using socket.io;

public class Login : MonoBehaviour
{

    Socket io;

    // Use this for initialization
    void Start ()
    {
        io = Socket.Connect ("http://localhost:7001/game");
        io.On ("connect", () => {
            print ("connect event");
            io.Emit ("userJoin", "123456789");
            io.Emit ("userJoin", "mdbj6");
            io.Emit ("userJoin", "a1");
            io.Emit ("userJoin", "1a");
            io.Emit ("userJoin", "987654321");

            io.Emit ("online", "123456789");
            io.Emit ("online", "mdbj6");
            io.Emit ("online", "a1");
            io.Emit ("online", "1a");
            io.Emit ("online", "987654321");

            //server side console:
            //userJoin token 123456789
            //userJoin token 987654321
            //online 123456789
            //online 987654321
        });
    }
}

Node.JS socket.io client

In the Node.js socket.io-client all going well:

const io = require('socket.io-client');
const socket = io('http://127.0.0.1:7001/game');

socket.on('connect', () => {
  console.log('connect!');
  socket.emit('userJoin', '123456789');
  socket.emit('userJoin', 'mdbj6');
  socket.emit('userJoin', 'a1');
  socket.emit('userJoin', '1a');
  socket.emit('userJoin', '987654321');

  socket.emit('online', '123456789');
  socket.emit('online', 'mdbj6');
  socket.emit('online', 'a1');
  socket.emit('online', '1a');
  socket.emit('online', '987654321');

});
//server side console:
//userJoin token 123456789
//userJoin token mdbj6
//userJoin token a1
//userJoin token 1a
//userJoin token 987654321
//online 123456789
//online mdbj6
//online a1
//online 1a
//online 987654321

server side

I use the egg.js framework and the egg.socket.io plugin, the egg.socket.io plugin is based on socket.io

exports.userJoin=function*(){
  const msg = this.args[0];
  console.log('userJoin',msg);
}
exports.online=function*(){
  const msg = this.args[0];
  console.log('online',msg);
}
gzlock commented 7 years ago

@ppz0th @smallmiro When the socket.io-client-unity3d client emit json format string , server can receive! LoginData.cs

[Serializable]
    public class LoginData
    {
        public string token;
        public string Oh;

        public string ToString ()
        {
            return JsonUtility.ToJson (this);
        }
    }

Login.cs

LoginData data = new LoginData ();
data.token = "1a2b3c";
data.Oh = "data";
io.On ("connect", () => {
    print ("connect event");
    io.Emit ("userJoin", data.ToString ());
    io.Emit ("userJoin", "123456789");
    io.Emit ("userJoin", "mdbj6");
    io.Emit ("userJoin", "a1");
    io.Emit ("userJoin", "1a");
    io.Emit ("userJoin", "987654321");

    io.Emit ("online", "123456789");
    io.Emit ("online", "mdbj6");
    io.Emit ("online", "a1");
    io.Emit ("online", "1a");
    io.Emit ("online", "987654321");
});

server console: image

Where did other messages go?

image

smallmiro commented 7 years ago

@gzlock Thanks, I'll check this problem. Plz give me a hours..

ppz0th commented 7 years ago

@gzlock Hello~ I've just check this issue and found a bug in packet-encoding. This issue will be fixed soon, Thanks~

Anyway the reason some packet is missing is an type casting failure in JavaSciprt. In your code, a string only has numeric char is sent successfully because JavaScript can deduce it as an integer value. However other strings (ex "mdbj6", "a1", "1a"), JavaScript can't deduce it to an specific type.

So as an test, if you embrace all data with "", for example "mdb6" => "\"mdb6\"", you can successfully sent all messages.

gzlock commented 7 years ago

@ppz0th So now it will be automatically added "\"bla bla bla\"" ? Hope it can!

ppz0th commented 7 years ago

Yeah. we're working on it~ Plz, wait for new release. Thanks :)

ppz0th commented 7 years ago

@gzlock v1.1.1 has been just released. This issue is fixed. See send and receive messages sample and Check Emit() and EmitJson() method usage.