php-PubNub服务器无法正确格式化消息

php-PubNub服务器无法正确格式化消息,第1张

概述我具有服务器配置,可以与Android客户端通话:<?phprequire_once("mysql.class.php");require_once("lib/autoloader.php");//SettingupthePubNubServer:usePubnub\Pubnub;$pubnub=newPubnub("pub-c...",##PUBLISH_KEY&quot

我具有服务器配置,可以与Android客户端通话:

<?PHPrequire_once("MysqL.class.PHP");require_once("lib/autoloader.PHP");// Setting up the PubNub Server:use Pubnub\Pubnub;$pubnub = new Pubnub(    "pub-c...",  ## PUBliSH_KEY    "sub-c..."  ## SUBSCRIBE_KEY);// Publishing :$post_data = Json_encode(array("type"=> "groupMessage", "data" => array("chatUser" => "SERVER", "chatMsg" => "Now lets talk", "chatTime"=>1446514201516)));$info = $pubnub->publish('MainChat', $post_data);print_r($info);print_r($post_data);?>

和HTML:

<!DOCTYPE HTML><HTML><head>    <Title>PubNub PHP Test Page</Title></head>    <body>        <form method="POST" action="index.PHP">            <input type="submit" name="submit" value="TestSendMessage" />        </form>    </body></HTML>

由于我可以看到消息到达客户端AndroID应用程序的日志控制台,因此该发布功能在服务器中起作用,但是该消息从未正确解析,因此在没有SubscribeCallback的情况下,它不会出现在列表视图中:

public voID subscribeWithPresence(String channel) {    this.channel = channel;    Callback subscribeCallback = new Callback() {        @OverrIDe        public voID successCallback(String channel, Object message) {            if (message instanceof JsONObject) {                try {                    JsONObject JsonObj = (JsONObject) message;                    JsONObject Json = JsonObj.getJsONObject("data");                    final String name = Json.getString(Constants.JsON_USER);                    final String msg = Json.getString(Constants.JsON_MSG);                    final long time = Json.getLong(Constants.JsON_TIME);                    if (name.equals(mpubNub.getUUID())) return; // Ignore own messages                    final ChatMessage chatMsg = new ChatMessage(name, msg, time);                    presentActivity.runOnUiThread(new Runnable() {                        @OverrIDe                        public voID run() {                            // Adding messages published to the channel                            mChatAdapter.addMessage(chatMsg);                        }                    });                } catch (JsONException e) {                    e.printstacktrace();                }            }            Log.d("PUBNUB", "Channel: " + channel + " Msg: " + message.toString());        }        @OverrIDe        public voID connectCallback(String channel, Object message) {            Log.d("Subscribe", "Connected! " + message.toString());            //hereNow(false);            // setStateLogin();        }    };    try {        mpubNub.subscribe(this.channel, subscribeCallback);        //presenceSubscribe();    } catch (PubnubException e) {        e.printstacktrace();        // Checking if success        Log.d("Fail subscribe ", "on channel: " + channel);    }}

通过单击TestSendMessage在浏览器中测试服务器输出,将产生:

Array ( [0] => 1 [1] => Sent [2] => 14465159776373950 ) {"type":"groupMessage","data":{"chatUser":"SERVER","chatMsg":"Now lets talk","chatTime":1446514201516}}

在应用程序中,从以下行输出日志:Log.d(“ PUBNUB”,“ Channel:” channel“ Msg:” message.toString());

返回值:D / PUBNUB:频道:MainChat消息:{“ type”:“ groupMessage”,“ data”:{“ chatUser”:“ SERVER”,“ chatMsg”:“ Now let talk”,“ chatTime”:1446514201516}}

确实应该这样,但是消息永远不会出现在消息的ListVIEw中,从而使JsON解析失败.

JsON标签在Constants类中很简单:

public static final String JsON_GROUP = "groupMessage";public static final String JsON_USER = "chatUser";public static final String JsON_MSG = "chatMsg";public static final String JsON_TIME = "chatTime";

如何重新配置​​服务器发送,以成功进行应用内解析?

解决方法:

通过PubNub发送JsON

Send the JSON object without stringifying it first.对于PHP,不要对消息进行Json_encode. PubNub SDK将为您编码和解码.

这个:

$post_data = array("type"=> "groupMessage", "data" => array(    "chatUser" => "SERVER", "chatMsg" => "Now lets talk",     "chatTime"=>1446514201516));

不是这个:

$post_data = Json_encode(array("type"=> "groupMessage", "data" => array(    "chatUser" => "SERVER", "chatMsg" => "Now lets talk",     "chatTime"=>1446514201516)));

请解决此问题.

总结

以上是内存溢出为你收集整理的php-PubNub服务器无法正确格式化消息全部内容,希望文章能够帮你解决php-PubNub服务器无法正确格式化消息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1090070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存