PubNub Server does not format message properly

2019-07-03 15:02发布

I have the server configuration to speak to the Android clients as:

<?php

require_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);

?>

and 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>

The publish function works in the server as I can see the messages arrive in the log console of the client Android app, but the message is never parsed correctly and therefore does not appear in the listview given the 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);
    }
}

Testing the server output in the browser by clicking TestSendMessage yields:

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

and in app the log output from line: Log.d("PUBNUB", "Channel: " + channel + " Msg: " + message.toString());

Returns: D/PUBNUB: Channel: MainChat Msg: {"type":"groupMessage","data":{"chatUser":"SERVER","chatMsg":"Now lets talk","chatTime":1446514201516}}

as it should, but the message never appears within the ListView of messages and thusly fails the JSON parsing.

The JSON tags are straightforward from the Constants class as:

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";

How can the server sending be reconfigured to allow the success of in app parsing?

1条回答
Summer. ? 凉城
2楼-- · 2019-07-03 15:41

Sending JSON over PubNub

Send the JSON object without stringifying it first. In the case for PHP, do not json_encode the message. PubNub SDK will encode and decode it for you.

This:

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

Not this:

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

Please comment if this resolves or not.

查看更多
登录 后发表回答