How to configure Photon Server to support json for websocket(Client use JavaScript SDK)?
I use JavaScript SDK to connect Photon Server through websocket,Connection can be established successfully,when sending data to the server,The Photon Server reports an error and closes the connection。
Error details:Photon.SocketServer.PeerBase - Disconnecting Peer T:ClientPeer,ConnId:39,ip:127.0.0.1:64273: Unexpected data received. Failure: Failed to parse operation request for protocol Json / encrypted: False Data: 7E-6D-7E-33-36-7E-6D-7E-7E-6A-7E-7B-22-72-65-71-22-3A-31-2C-22-76-61-6C-73-22-3A-5B-22-61-3A-20-61-61-22-2C-22-62-3A-62-62-22-5D-7D
Best Answer
-
With peer API, you can send arbitrary messages if your server-side code can handle them but some restrictions still apply. Server recognizes only a dictionary with numeric keys as a payload. But you send an array of strings instead: var data = ["a: aa","b:bb"];. You can pass a dictionary with string key as a value of top-level dictionary:
var data = {1: "aa", 2: "bb", 3: {"a": "aa", "b": "bb" }};
0
Answers
-
This is not a valid client message. How do you send it?
Can you reproduce this with a demo from the SDK package?
0 -
I use the demo program created by cocos creator,not the demo from SDK package.
I thought that after I call sendOperation(),the server can receive it without disconnecting the connection with the client,even if the server cannot parse the data.
this is my code:
const {ccclass, property} = cc._decorator;
@ccclass
export default class Helloworld extends cc.Component {
public MyPhotonPeer:Photon.PhotonPeer;
start () {
this.MyPhotonPeer = new Photon.PhotonPeer(Photon.ConnectionProtocol.Ws,"127.0.0.1:9090","","");
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.connecting,this.PeerStatusCallBack_Connecting.bind(this));
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.connect,this.PeerStatusCallBack_Connect.bind(this));
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.connectFailed,this.PeerStatusCallBack_ConnectFailed.bind(this));
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.disconnect,this.PeerStatusCallBack_Disconnect.bind(this));
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.connectClosed,this.PeerStatusCallBack_ConnectClosed.bind(this));
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.error,this.PeerStatusCallBack_Error.bind(this));
this.MyPhotonPeer.addPeerStatusListener(Photon.PhotonPeer.StatusCodes.timeout,this.PeerStatusCallBack_Timeout.bind(this));
this.MyPhotonPeer.addResponseListener(1,this.Peer_ResponseListener.bind(this));
this.MyPhotonPeer.addEventListener(1,this.Peer_EventListener.bind(this));
this.MyPhotonPeer.connect("MyGame1");
}
PeerStatusCallBack_Connect()
{
console.log('Connect Success');
this.sendData();
}
//send test data to server
sendData()
{
try
{
var data = ["a: aa","b:bb"];
this.MyPhotonPeer.sendOperation(1,data);
}
catch (error)
{
console.log("Send Operaton Error=" + error);
}
}
Peer_EventListener(EventContent)
{
console.log('Event=' + EventContent);
}
Peer_ResponseListener(RspContent)
{
console.log('Rsp=' + RspContent);
}
//PhotonPeer Status callback
PeerStatusCallBack_Connecting()
{
console.log('Connecting');
}
PeerStatusCallBack_ConnectFailed()
{
console.log('ConnectFailed');
}
PeerStatusCallBack_Disconnect()
{
console.log('Disconnect');
}
PeerStatusCallBack_ConnectClosed()
{
console.log('ConnectClosed');
}
PeerStatusCallBack_Error()
{
console.log('Connect Error');
}
PeerStatusCallBack_Timeout()
{
console.log('Connect Timeout');
}
}
0 -
With peer API, you can send arbitrary messages if your server-side code can handle them but some restrictions still apply. Server recognizes only a dictionary with numeric keys as a payload. But you send an array of strings instead: var data = ["a: aa","b:bb"];. You can pass a dictionary with string key as a value of top-level dictionary:
var data = {1: "aa", 2: "bb", 3: {"a": "aa", "b": "bb" }};
0 -
Thank you! it work😀
0