简体   繁体   English

Socket.IO发射对象Javascript

[英]Socket.IO Emit Object Javascript

am not able to emit object from client to server.. here's the code on my client 无法从客户端到服务器发射对象。这是客户端上的代码

var foo = {
name: xxx,
type: mytype
};
cc.log(foo);
socket.emit('test', foo);

ERROR 错误

JS: [object Object]
JSB SocketIO.emit method called
jsb: ERROR: File /Users/xxx/Sites/socketIO/frameworks/js-bindings/bindings/manual/network/jsb_socketio.cpp: Line: 272, Function: js_cocos2dx_SocketIO_emit
Error processing arguments

You can't take a javascript object "foo" as the second parameter. 您不能将javascript对象“ foo”作为第二个参数。 As it is defined in SocketIO.h: 如SocketIO.h中所定义:

void emit(std::string eventname, std::string args);

The second parameter is a string. 第二个参数是一个字符串。 Cocos2d-js can't convert a javascript object "foo" to std::string automatically. Cocos2d-js无法将JavaScript对象“ foo”自动转换为std :: string。 However, cocos2d-js can convert a javascript string to std::string by jsval_to_std_string(). 但是,cocos2d-js可以通过jsval_to_std_string()将JavaScript字符串转换为std :: string。

If you want to send object, you can send object as a string and do decoding on receiving it. 如果要发送对象,则可以将对象作为字符串发送,并在接收时进行解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM