-

   rss_rss_hh_full

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 1

:


PubNub:

, 26 2017 . 10:45 +
Cromathaar 10:45

PubNub:

  • Tutorial
image

, ( ) PubNub. , 2010- , Global Data Stream Network (DSN), IaaS-, . Distillery development- PubNub, , PubNub demo-, .

, (C# + JavaScript), GitHub. , , PubNub, , .

PubNub :

  • Realtime Messaging. API, Publish/Subscribe, , 15 latency 250. , , , .
  • Presence. API / .
  • Functions. BLOCKS, (, ). , JavaScript PubNub, , , , , .

PubNub 70- SDK , IoT- Arduino, RaspberryPi Samsung Smart TV ( ).

, , . , , : PubNub, SDK : Presence, PAM BLOCK. PAM PubNub Access Manager , , . , , , . , , .

, -. , - , ReadOnly . , Vasya joined the channel, . , . BLOCK IBM Watson, , . : en-US_AllisonVoice (), en-US_LisaVoice () en-US_MichaelVoice (). Allison, , , .

. , , - :

public class HomeController : Controller
{
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Main(LoginDTO loginDTO)
    {
        String chatChannel = ConfigurationHelper.ChatChannel;
        String textToSpeechChannel = ConfigurationHelper.TextToSpeechChannel;
        String authKey = loginDTO.Username + DateTime.Now.Ticks.ToString();

        var chatManager = new ChatManager();
            
        if (loginDTO.ReadAccessOnly)
        {
            chatManager.GrantUserReadAccessToChannel(authKey, chatChannel);
        }
        else
        {
            chatManager.GrantUserReadWriteAccessToChannel(authKey, chatChannel);
        }

        chatManager.GrantUserReadWriteAccessToChannel(authKey, textToSpeechChannel);

        var authDTO = new AuthDTO()
        {
            PublishKey = ConfigurationHelper.PubNubPublishKey,
            SubscribeKey = ConfigurationHelper.PubNubSubscribeKey,
            AuthKey = authKey,
            Username = loginDTO.Username,
            ChatChannel = chatChannel,
            TextToSpeechChannel = textToSpeechChannel
        };

        return View(authDTO);
    }
}

Main DTO , ( , IBM Watson), ChatManager . . ChatManager, SDK PubNub:

public class ChatManager
{
    private const String PRESENCE_CHANNEL_SUFFIX = "-pnpres";

    private Pubnub pubnub;

    public ChatManager()
    {
        var pnConfiguration = new PNConfiguration();
        pnConfiguration.PublishKey = ConfigurationHelper.PubNubPublishKey;
        pnConfiguration.SubscribeKey = ConfigurationHelper.PubNubSubscribeKey;
        pnConfiguration.SecretKey = ConfigurationHelper.PubNubSecretKey;
        pnConfiguration.Secure = true;

        pubnub = new Pubnub(pnConfiguration);
    }

    public void ForbidPublicAccessToChannel(String channel)
    {
        pubnub.Grant()
            .Channels(new String[] { channel })
            .Read(false)
            .Write(false)
            .Async(new AccessGrantResult());
    }

    public void GrantUserReadAccessToChannel(String userAuthKey, String channel)
    {
        pubnub.Grant()
            .Channels(new String[] { channel, channel + PRESENCE_CHANNEL_SUFFIX })
            .AuthKeys(new String[] { userAuthKey })
            .Read(true)
            .Write(false)
            .Async(new AccessGrantResult());
    }

    public void GrantUserReadWriteAccessToChannel(String userAuthKey, String channel)
    {
        pubnub.Grant()
            .Channels(new String[] { channel, channel + PRESENCE_CHANNEL_SUFFIX })
            .AuthKeys(new String[] { userAuthKey })
            .Read(true)
            .Write(true)
            .Async(new AccessGrantResult());
    }
}

PRESENCE_CHANNEL_SUFFIX. , Presence , -pnpres. , PubNub Access Manager, Grant, Presence- .

var pubnub;
var chatChannel;
var textToSpeechChannel;
var username;

function init(publishKey, subscribeKey, authKey, username, chatChannel, textToSpeechChannel) {
    pubnub = new PubNub({
        publishKey: publishKey,
        subscribeKey: subscribeKey,
        authKey: authKey,
        uuid: username
    });

    this.username = username;
    this.chatChannel = chatChannel;
    this.textToSpeechChannel = textToSpeechChannel;

    addListener();
    subscribe();
}

, JavaScript- SDK. . , Presence IBM Watson. :

function subscribe() {
    pubnub.subscribe({
        channels: [chatChannel, textToSpeechChannel],
        withPresence: true
    });
}

subscribe , addListener :

function addListener() {
    pubnub.addListener({
        status: function (statusEvent) {
            if (statusEvent.category === "PNConnectedCategory") {
                getOnlineUsers();
            }
        },
        message: function (message) {
            if (message.channel === chatChannel) {
                var jsonMessage = JSON.parse(message.message);
                var chat = document.getElementById("chat");
                if (chat.value !== "") {
                    chat.value = chat.value + "\n";
                    chat.scrollTop = chat.scrollHeight;
                }

                chat.value = chat.value + jsonMessage.Username + ": " + 
                    jsonMessage.Message;
            }
            else if (message.channel === textToSpeechChannel) {
                if (message.publisher !== username) {
                    var audio = new Audio(message.message.speech);
                    audio.play();
                }
            }
        },
        presence: function (presenceEvent) {
            if (presenceEvent.channel === chatChannel) {
                if (presenceEvent.action === 'join') {
                    if (!UserIsOnTheList(presenceEvent.uuid)) {
                        AddUserToList(presenceEvent.uuid);
                    }

                    PutStatusToChat(presenceEvent.uuid, 
                        "joins the channel");
                }
                else if (presenceEvent.action === 'timeout') {
                    if (UserIsOnTheList(presenceEvent.uuid)) {
                        RemoveUserFromList(presenceEvent.uuid);
                    }

                    PutStatusToChat(presenceEvent.uuid, 
                        "was disconnected due to timeout");
                }
            }
        }
    });
}

-, PNConnectedCategory, . , , Presence- join . -, , , , , Audio IBM Watson - .

:

function publish(message) {
    var jsonMessage = {
        "Username": username,
        "Message": message
    };

    var publishConfig = {
        channel: chatChannel,
        message: JSON.stringify(jsonMessage)
    };

    pubnub.publish(publishConfig);

    var emotedText = '';

    var selectedEmotion = iconSelect.getSelectedValue();

    if (selectedEmotion !== "") {
        emotedText += '';
    }

    emotedText += message;

    if (selectedEmotion !== "") {
        emotedText += '';
    }

    emotedText += '';

    jsonMessage = {
        "text": emotedText
    };

    publishConfig = {
        channel: textToSpeechChannel,
        message: jsonMessage
    };

    pubnub.publish(publishConfig);
}

, , SDK, . . , IBM Watson. Speech Synthesis Markup Language (SSML), . , ReadOnly-, PAM .

, PubNub, , , Insteon Curago. , GitHub.
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/338720/

:  

: [1] []
 

:
: 

: ( )

:

  URL