void FetchMessages(PubNub pubnub, List <string> listChannels) { pubnub.FetchMessages().Channels(listChannels).IncludeMeta(true).Async((result, status) => { //pubnub.FetchMessages().Channels(new List<string>{"channel2"}).Async ((result, status) => { if (status.Error) { Debug.Log(string.Format("In Example, FetchMessages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category)); } else { Debug.Log(string.Format("In FetchMessages, result: ")); //,result.EndTimetoken, result.Messages[0].ToString())); foreach (KeyValuePair <string, List <PNMessageResult> > kvp in result.Channels) { Debug.Log("kvp channelname" + kvp.Key); foreach (PNMessageResult pnMessageResut in kvp.Value) { Debug.Log("Channel: " + pnMessageResut.Channel); Debug.Log("payload: " + pnMessageResut.Payload.ToString()); Debug.Log("timetoken: " + pnMessageResut.Timetoken.ToString()); Display(string.Format("Channel {0}, payload {1}, timetoken {2}", pnMessageResut.Channel, pnMessageResut.Payload.ToString(), pnMessageResut.Timetoken.ToString())); } } } }); }
void FetchRecursive(long start, List <string> listChannels) { pubnub.FetchMessages().Channels(listChannels).Start(start).Async((result, status) => { if (status.Error) { Debug.Log(string.Format("In Example, FetchMessages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category)); } else { Debug.Log(string.Format("In FetchMessages, result: ")); foreach (KeyValuePair <string, List <PNMessageResult> > kvp in result.Channels) { Debug.Log("kvp channelname" + kvp.Key); foreach (PNMessageResult pnMessageResut in kvp.Value) { Debug.Log("Channel: " + pnMessageResut.Channel); Debug.Log("payload: " + pnMessageResut.Payload.ToString()); Debug.Log("timetoken: " + pnMessageResut.Timetoken.ToString()); } } } }); }
void Start() { // Use this for initialization PNConfiguration pnConfiguration = new PNConfiguration(); pnConfiguration.PublishKey = "demo"; pnConfiguration.SubscribeKey = "demo"; pnConfiguration.LogVerbosity = PNLogVerbosity.BODY; pnConfiguration.UUID = "user-1"; pubnub = new PubNub(pnConfiguration); // Add Listener to Submit button to send messages Button btn = SubmitButton.GetComponent <Button>(); btn.onClick.AddListener(TaskOnClick); // Fetch the maxMessagesToDisplay messages sent on the given PubNub channel pubnub.FetchMessages() .Channels(new List <string> { channel }) .Count(maxMessagesToDisplay) .Async((result, status) => { if (status.Error) { Debug.Log(string.Format( " FetchMessages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category )); } else { foreach (KeyValuePair <string, List <PNMessageResult> > kvp in result.Channels) { foreach (PNMessageResult pnMessageResult in kvp.Value) { // Format data into readable format JSONInformation chatmessage = JsonUtility.FromJson <JSONInformation>(pnMessageResult.Payload.ToString()); // Call the function to display the message in plain text CreateChat(chatmessage); } } } }); // This is the subscribe callback function where data is recieved that is sent on the channel pubnub.SubscribeCallback += (sender, e) => { SubscribeEventEventArgs message = e as SubscribeEventEventArgs; if (message.MessageResult != null) { // Format data into a readable format JSONInformation chatmessage = JsonUtility.FromJson <JSONInformation>(message.MessageResult.Payload.ToString()); // Call the function to display the message in plain text CreateChat(chatmessage); // When a new chat is created, remove the first chat and transform all the messages on the page up SyncChat(); } }; // Subscribe to a PubNub channel to receive messages when they are sent on that channel pubnub.Subscribe() .Channels(new List <string>() { channel }) .WithPresence() .Execute(); }
void Start() { // Use this for initialization PNConfiguration pn_configuration = new PNConfiguration(); pn_configuration.PublishKey = "pub-c-c9fedf8b-6c91-46cf-b6d9-14b5ec353a21"; pn_configuration.SubscribeKey = "sub-c-5b8e3a72-8796-11ea-b883-d2d532c9a1bf"; pn_configuration.LogVerbosity = PNLogVerbosity.BODY; pn_configuration.UUID = System.Guid.NewGuid().ToString(); pubnub = new PubNub(pn_configuration); // Add listener to submit button to send messages Button btn = submit_button.GetComponent <Button>(); btn.onClick.AddListener(task_on_click); // Fetch the last 13 messages sent on the given PubNub channel pubnub.FetchMessages().Channels(new List <string> { "chatchannel13" }).Count(10).Async((result, status) => { if (status.Error) { Debug.Log(string.Format("Fetch Messages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category)); } else { foreach (KeyValuePair <string, List <PNMessageResult> > kvp in result.Channels) { foreach (PNMessageResult pn_message_result in kvp.Value) { // Format Data into readable format JSONInformation chatmessage = JsonUtility.FromJson <JSONInformation>(pn_message_result.Payload.ToString()); // Call the function to display the message in plaintext create_chat(chatmessage); // Counter used for positioning the text UI if (counter != 200) { counter += 20; } } } } }); // Subscribe to a PubNub Channel to receive messages when they are sent on that channel pubnub.Subscribe().Channels(new List <string>() { "chatchannel13" }).WithPresence().Execute(); // This is the subscribe callback function where data is received that is received on the channel pubnub.SubscribeCallback += (sender, e) => { SubscribeEventEventArgs message = e as SubscribeEventEventArgs; if (message.MessageResult != null) { // Format data into readable format JSONInformation chatmessage = JsonUtility.FromJson <JSONInformation>(message.MessageResult.Payload.ToString()); // Call the function to display the message in plaintext create_chat(chatmessage); // When a new Chat is created, remove the first chat and transform all the messages on the page up sync_chat(); // Counter used for positioning the text UI if (counter != 200) { counter += 20; } } }; }