java/srcPubnubApi/com/pubnub/api/PubnubCore.java
/**
     * Send a message to a channel.
     *
     * @param args
     *            Hashtable containing channel name, message, callback
     */
Send a message to a channel.
@param args
Hashtable containing channel name, message, callback
Send a message to a channel . @param args Hashtable containing channel name message callback
protected void publish(Hashtable args) {
        final String channel = (String) args.get("channel");
        final Object message = args.get("message");
        final Callback callback = getWrappedCallback((Callback) args.get("callback"));
        String storeInHistory = (String) args.get("storeInHistory");
        String msgStr = message.toString();
        Hashtable parameters = PubnubUtil.hashtableClone(params);
        if (storeInHistory != null && storeInHistory.length() > 0) parameters.put("store", storeInHistory);
        if (this.CIPHER_KEY.length() > 0) {
            PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.IV);
            try {
                if (message instanceof String) {
                    msgStr = "\"" + msgStr + "\"";
                }
                msgStr = "\"" + pc.encrypt(msgStr) + "\"";
            } catch (PubnubException e) {
                callback.errorCallback(channel, getPubnubError(e, PubnubError.PNERROBJ_ENCRYPTION_ERROR, 4, msgStr + " : " + e.toString()));
                return;
            }
        } else {
            if (message instanceof String) {
                msgStr = "\"" + msgStr + "\"";
            }
        }
        String signature = "0";
        if (this.SECRET_KEY.length() > 0) {
            StringBuffer string_to_sign = new StringBuffer();
            string_to_sign.append(this.PUBLISH_KEY).append('/')
            .append(this.SUBSCRIBE_KEY).append('/')
            .append(this.SECRET_KEY).append('/').append(channel)
            .append('/').append(msgStr);
            try {
                signature = new String(PubnubCrypto.hexEncode(PubnubCrypto
                        .md5(string_to_sign.toString())), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                PubnubError pe = PubnubError.getErrorObject(PubnubError.PNERROBJ_ENCRYPTION_ERROR, 6, msgStr + " : " + e.toString());
                callback.errorCallback(channel, pe);
            } catch (PubnubException e) {
                callback.errorCallback(channel, getPubnubError(e, PubnubError.PNERROBJ_ENCRYPTION_ERROR, 5, msgStr + " : " + e.toString()));
            }
        }
        String[] urlComponents = { getPubnubUrl(), "publish", this.PUBLISH_KEY,
                this.SUBSCRIBE_KEY, PubnubUtil.urlEncode(signature),
                PubnubUtil.urlEncode(channel), "0",
                PubnubUtil.urlEncode(msgStr)
        };
        class PublishResponseHandler extends ResponseHandler {
            public void handleResponse(HttpRequest hreq, String response) {
                JSONArray jsarr;
                try {
                    jsarr = new JSONArray(response);
                } catch (JSONException e) {
                    handleError(hreq,
                            PubnubError.getErrorObject(PubnubError.PNERROBJ_INVALID_JSON, 1, response));
                    return;
                }
                callback.successCallback(channel, jsarr);
            }
            public void handleError(HttpRequest hreq, PubnubError error) {
                callback.errorCallback(channel, error);
                return;
            }
        }
        HttpRequest hreq = new HttpRequest(urlComponents, parameters, new PublishResponseHandler());
        _request(hreq, nonSubscribeManager);
    }
protected void publish ( Hashtable args ) { final String channel = ( String ) args . get ( "channel" ) ; final Object message = args . get ( "message" ) ; final Callback callback = getWrappedCallback ( ( Callback ) args . get ( "callback" ) ) ; String storeInHistory = ( String ) args . get ( "storeInHistory" ) ; String msgStr = message . toString ( ) ; Hashtable parameters = PubnubUtil . hashtableClone ( params ) ; if ( storeInHistory != null && storeInHistory . length ( ) > 0 ) parameters . put ( "store" , storeInHistory ) ; if ( this . CIPHER_KEY . length ( ) > 0 ) { PubnubCrypto pc = new PubnubCrypto ( this . CIPHER_KEY , this . IV ) ; try { if ( message instanceof String ) { msgStr = "\"" + msgStr + "\"" ; } msgStr = "\"" + pc . encrypt ( msgStr ) + "\"" ; } catch ( PubnubException e ) { callback . errorCallback ( channel , getPubnubError ( e , PubnubError . PNERROBJ_ENCRYPTION_ERROR , 4 , msgStr + " : " + e . toString ( ) ) ) ; return ; } } else { if ( message instanceof String ) { msgStr = "\"" + msgStr + "\"" ; } } String signature = "0" ; if ( this . SECRET_KEY . length ( ) > 0 ) { StringBuffer string_to_sign = new StringBuffer ( ) ; string_to_sign . append ( this . PUBLISH_KEY ) . append ( '/' ) . append ( this . SUBSCRIBE_KEY ) . append ( '/' ) . append ( this . SECRET_KEY ) . append ( '/' ) . append ( channel ) . append ( '/' ) . append ( msgStr ) ; try { signature = new String ( PubnubCrypto . hexEncode ( PubnubCrypto . md5 ( string_to_sign . toString ( ) ) ) , "UTF-8" ) ; } catch ( UnsupportedEncodingException e ) { PubnubError pe = PubnubError . getErrorObject ( PubnubError . PNERROBJ_ENCRYPTION_ERROR , 6 , msgStr + " : " + e . toString ( ) ) ; callback . errorCallback ( channel , pe ) ; } catch ( PubnubException e ) { callback . errorCallback ( channel , getPubnubError ( e , PubnubError . PNERROBJ_ENCRYPTION_ERROR , 5 , msgStr + " : " + e . toString ( ) ) ) ; } } String [ ] urlComponents = { getPubnubUrl ( ) , "publish" , this . PUBLISH_KEY , this . SUBSCRIBE_KEY , PubnubUtil . urlEncode ( signature ) , PubnubUtil . urlEncode ( channel ) , "0" , PubnubUtil . urlEncode ( msgStr ) } ; class PublishResponseHandler extends ResponseHandler { public void handleResponse ( HttpRequest hreq , String response ) { JSONArray jsarr ; try { jsarr = new JSONArray ( response ) ; } catch ( JSONException e ) { handleError ( hreq , PubnubError . getErrorObject ( PubnubError . PNERROBJ_INVALID_JSON , 1 , response ) ) ; return ; } callback . successCallback ( channel , jsarr ) ; } public void handleError ( HttpRequest hreq , PubnubError error ) { callback . errorCallback ( channel , error ) ; return ; } } HttpRequest hreq = new HttpRequest ( urlComponents , parameters , new PublishResponseHandler ( ) ) ; _request ( hreq , nonSubscribeManager ) ; }
Send a message to a channel.
Send a message to a channel .
// Encrypt Message // Generate String to Sign // Sign Message