Is there any way to send or receive images, audio and video using Photon Pun2 or Photon Chat? - unity3d

I am using both packages Photon Pun2 and Photon Chat in my application. But i can't find any way to send or receive images, audio, or video via private message.

Yes there is ... However, it is quite limited.
Exactly how big are the things you have to send?
Chat
In Photon Chat Message payload is limited to something between 400.000 and 500.000 bytes. I didn't test it more in detail but if your message size hits a certain limit you are immediately disconnected without a proper feadback for the reason ^^
See Photon Chat Intro
Example
public class MyChatListner : IChatClientListener
{
public event Action<byte[]> OnReceived;
private ChatClient cient;
public void Initialize()
{
client = new ChatClient(this);
client.ChatRegion = ...;
client.Connect(....);
}
// Sicne you already work with the chat you should know but anyway
// This has to be called continuously (who knows in what intervals)
public void Heartbeat()
{
client.Service();
}
public void SendData(string recipient, byte[] data)
{
client.SendPrivateMessage(recipient, data);
}
public void OnPrivateMessage(string sender, object message, string channelName)
{
OnReceived?.Invoke((byte[])message);
}
// also will have to implement the other methods from IChatClientListener ...
}
PUN2
In Pun2 I don't know if such a limit exists but it will definitely delay everything else until the file is fully received. Here you can directly send and receive byte[] via PhotonNetwork.RaiseEvent and OnEvent (via the interface IOnEventCallback)
Example
// doesn't even have to be monobehaviour necessarily
public class FileTransmitter : IOnEventCallback
{
private const byte eventID = 42;
public event Action<byte[]> OnReceived;
public void Enable()
{
PhotonNetwork.AddCallbackTarget(this);
}
public void Disable()
{
PhotonNetwork.RemoveCallbackTarget(this);
}
public void SendData(byte[] data, SendOptions sendoptions, RaiseEventOptions raiseEventOptions = null)
{
PhotonNetwork.RaiseEvent(eventID, data, raiseEventOptions, sendoptions);
}
public void OnEvent(EventData photonEvent)
{
if(photonEvent.Code != eventID) return;
var data = (byte[]) photonEvent.CustomData;
OnReceived?.Invoke(data);
}
}
Alternatively you can of course also use the RPC directly and use e.g.
public class FileTransfer : MonoBehaviourPun
{
[PunRPC]
void ChatMessage(string fileName, byte[] content)
{
// e.g.
File.WriteAllBytes(Path.Combine(Application.persistentDataPath, fileName), content);
}
public void SendFile(string fileName, byte[] content, PhotonPlayer targetPlayer)
{
photonView.RPC(nameof(ChatMessage), targetPlayer, fileName, content);
}
}
Either way personally what I did was using only one single byte[] and encoded all required information into it. Photon will do this anyway with all parameters but if you already know exactly what data you send and how to deserialize it it is way more efficient to do it yourself e.g. in a Thread/Task. Then on the receiver side I deserialized these into the individual information again.

It's possible and working just fine (even though you should watch for the limits as #derHugo said). My implementation uses RPC to send an array of bytes using Texture2D.EncodeToPNG and then decoding it on the clients upon receiving it.
To convert the byte array back into the image, you can use Texture2D.LoadImage method.
The approach is similar for sending other multimedia types, just the encoding/decoding methods are different.

PhotonStream allows you to stream any byte[] data though the network.
In theory, any data type can be converted in to byte[], and decode them on the receiver side.
Below example script is just an example with minimal setup.
reference from: https://frozenmist.com/docs/apis/fmetp-stream/pun2-example/
It's also possible to live stream video, audio and remote commands with popular third party plugins like FMETP STREAM, which has native fast encoder for capturing your Game View and Desktop View in real time, even compatible with mobile and VR devices.
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ref: https://frozenmist.com/docs/apis/fmetp-stream/pun2-example/
public class FMStreamPUN : Photon.Pun.MonoBehaviourPun, IPunObservable {
private Queue<byte[]> appendQueueSendData = new Queue<byte[]>();
public int appendQueueSendDataCount { get { return appendQueueSendData.Count; } }
public UnityEventByteArray OnDataByteReadyEvent = new UnityEventByteArray();
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if (stream.IsWriting) {
//Send the meta data of the byte[] queue length
stream.SendNext(appendQueueSendDataCount);
//Sending the queued byte[]
while(appendQueueSendDataCount > 0) {
byte[] sentData = appendQueueSendData.Dequeue();
stream.SendNext(sentData);
}
}
if (stream.IsReading) {
if (!photonView.IsMine) {
//Get the queue length
int streamCount = (int)stream.ReceiveNext();
for (int i = 0; i < streamCount; i++) {
//reading stream one by one
byte[] receivedData = (byte[])stream.ReceiveNext();
OnDataByteReadyEvent.Invoke(receivedData);
}
}
}
}
public void Action_SendData(byte[] inputData) {
//inputData(byte[]) is the encoded byte[] from your encoder
//doesn't require any stream, when there is only one player in the room
if(PhotonNetwork.CurrentRoom.PlayerCount > 1) appendQueueSendData.Enqueue(inputData);
}
}

Related

Using UnityWebRequest to get response body in a static class

Our WebGL game made using Unity requires getting data from the body of a GET response. Currently this is implemented using a static class, with a blocking while loop to wait for the request to be done:
public static class NetworkController {
public MyDataClass GetData() {
UnityWebRequest webRequest = UnityWebRequest.Get(...);
webRequest.SendWebRequest();
while (!webRequest.isDone) {} // block here
// Get body of response webRequest.downloadHandler.text and
// return a MyDataClass object
}
}
This works in the editor until later we discovered using WebGL disallows blocking the web request. Unity suggests using coroutines to mitigate that, but StartCoroutine() only exists in the MonoBehaviour class which our static class cannot inherit from.
Is there a way to continue the execution only after the web request gets a response while using a static class?
As per comments, the code is modified so now the static class makes a call to a MonoBehaviour singleton:
public static class NetworkController {
public MyDataClass GetData() {
return NetworkControllerHelper.GetInstance().GetData();
}
}
public class NetworkControllerHelper : MonoBehaviour {
/* ... GetInstance() method for creating a GameObject and create & return an instance... */
private MyDataClass coroutineResult = null;
public MyDataClass GetData() {
UnityWebRequest webRequest = UnityWebRequest.Get(...);
StartCoroutine(WebRequestCoroutine(webRequest));
// How do I detect that the coroutine has ended then continue without hanging the program?
while (coroutineResult == null) {}
return coroutineResult;
}
// Coroutine for trying to get a MyDataClass object
private IEnumerator WebRequestCoroutine(UnityWebRequest webRequest) {
yield return webRequest.SendWebRequest();
while (!webRequest.isDone) yield return new WaitForSeconds(0.1f);
// Creating the MyDataClassObject
coroutineResult = ...;
yield break;
}
}
We need to wait for the resulting MyDataClass to be created, so we check if coroutineResult is null, but using a while loop to block hangs the program. How do I wait for a some seconds before checking for the loop condition?
You can't load a resource synchronously in WebGL as explained here.
Do not block on WWW or WebRequest downloads
Do not use code which blocks on a WWW or WebRequest download, like this:
while(!www.isDone) {}
Although, I cannot find a proper solution, I need 'webRequest.downloadHandler.text' and I want to use this string globally on another classes.

UDP Client - how to close after BeginReceive

I need to receive small UDP packets from within my ERP system (Dynamics Nav). I created a class with some code from the internet (first time I use code for UDP), added a COM interface and use the resulting DLL within Nav. It worked right away, but I realized that the udp.Close() function did not really close the thing, and I couldn´t instantiate the class a second time.
After looking at many topics about udp.BeginReceive() at stackoverflow and on other sites, I understand the reason for that. There needs to be a final udp.EndReceive call before closing the upd object.
Since the class is running in the background its using a callback function defined by udp.BeginReceive. The callback function then gets the data by calling udp.EndReceive and finally stores it to a string. Nav can retrieve that string whenever it wants that data trough a simple property.
public string GetMessage { get { return(message); } }
public void Start()
{
udp = new UdpClient(PORT_NUMBER);
StartListening();
}
public void Stop()
{
udp.Close();
}
private void StartListening()
{
ar_ = udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER);
byte[] bytes = udp.EndReceive(ar, ref ip);
message = Encoding.ASCII.GetString(bytes);
StartListening();
}
Everthing is fine, except ...
Nav calls Start() which issues StartListening(), which defines the callback function. After receiving the data through udp.EndReceive it calls StartListening() again - this part is working fine.
As soon, as Nav calls the Stop() function however, the trouble starts, and I understand that this is, bacause there is no final call to EndReceive and thus an open session.
One may say, why don´t do an EndReceive() within the Stop() function before udp.Close()? Well, because I couldn´t find the correct parameters for that call.
Actually I do have a working class. I don´t try to close the session within the Stop() function but instead set a bool variable. The next time the callback function is issued, depending on that bool it doesn´t do a StartListening() but a upd.CLose() instead. And finally, to make sure there will be data to issue the callback function, I call my Send() function sending some single character.
While the following code is working perfectly, I know it´s kind of crazy:
public string GetMessage { get { return(message); } }
public void Start()
{
active = true;
udp = new UdpClient(PORT_NUMBER);
StartListening();
}
public void Stop()
{
active = false; // force callback function to close session
Send("x"); // issue callback function ... crazy
//udp.Close();
}
private void StartListening()
{
ar_ = udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER);
byte[] bytes = udp.EndReceive(ar, ref ip);
message = Encoding.ASCII.GetString(bytes);
if (active) StartListening(); else udp.Close();
}
Does anyone have a hint how to issue EndReceive() within my Stop() function before calling udp.Close()?
Thanks in advance
Michael
I recently found a solution just by using a Thread() instead of the async receive - works perfectly so far:
public class Bos_UDP
{
private UdpClient udp;
const int PORT_NUMBER = 15000;
private String message = "";
public Thread receiveThread;
public string GetMessage { get { return(message); } }
public void Start()
{
udp = new UdpClient(PORT_NUMBER);
receiveThread = new Thread(ThreadReceive);
receiveThread.Start();
}
public void Stop()
{
receiveThread.Abort(new object());
udp.Close();
}
public void ThreadReceive()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER);
while (true)
{
var data = udp.Receive(ref ip);
message = Encoding.Default.GetString(data);
}
}
public void Send(string message)
{
UdpClient client = new UdpClient();
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("255.255.255.255"), PORT_NUMBER);
byte[] bytes = Encoding.ASCII.GetBytes(message);
client.Send(bytes, bytes.Length, ip);
client.Close();
}
}

Is there any counter observable through Rx?

I am looking for something I would call CounterObservable One side would count the numbers on it and other side would be the observer side that will receive notification every time total count changes.
In other words I will have something like this
public CounterObservable totalMailsReceived = new CounterObservable(0);
public void OnNewMail(Mail mail)
{
totalMailsReceived++;
///Rest of the code goes here
}
on the Observer side I will have
mailManager.totalMailsReceived.Subscribe(count => labelCount.Text = count.ToString());
Or if I want to go real classy, I would use Paul Betts' ReactiveUI like the following
mailManager.totalMailsReceived.ToProperty(x => x.TotalMailsReceived);
I have so far found nothing in Rx that could help me. But I figured if I create my own class that implements IObservable<int>. I am thinking of leveraging the Sample MSDN Code for IObservable implementation for that.
My questions are
1. Is that MSDN Sample thread-safe ?
2. Is there really nothing in Rx already that does what I am trying to do ?
Just use a BehaviorSubject:
public class MailServer
{
private BehaviorSubject<int> _count = new BehaviorSubject<int>(0);
public IObservable<int> TotalMailsReceived
{
get { return _count; }
}
public void OnNewMail(Mail mail)
{
_count.OnNext(_count.Value + 1);
}
}
Or, if you decide to go deeper into Rx, so that you are just observing a Mail stream, then you can use Scan operator which is good for that and Publish to remember the most recent value and multicast it to all subscribers.
You can write this new extension method:
public IObservable<T> RunningTotal<T>(this IObservable<T> source)
{
return source.Scan(0, sum => sum + 1);
}
And use it like so:
public class MailServer
{
private IConnectableObservable<int> _total;
private IDisposable _subscription;
public MailServer(IObservable<Mail> incomingMail)
{
_total = incomingMail.RunningTotal().Publish(0);
_subscription = _total.Connect();
}
public IObservable<int> TotalMailsReceived
{
get { return _total; }
}
}

Chain multiple 2 step file uploads with Rx

I am attempting to upload multiple files from a Silverlight client directly to Amazon S3. The user chooses the files from the standard file open dialog and I want to chain the uploads so they happen serially one at a time. This can happen from multiple places in the app so I was trying to wrap it up in a nice utility class that accepts an IEnumerable of the chosen files exposes an IObservable of the files as they are uploaded so that the UI can respond accordingly as each file is finished.
It is fairly complex due to all the security requirements of both Silverlight and AmazonS3. I'll try to briefly explain my whole environment for context, but I have reproduced the issue with a small console application that I will post the code to below.
I have a 3rd party utility that handles uploading to S3 from Silverlight that exposes standard event based async methods. I create one instance of that utility per uploaded file. It creates an unsigned request string that I then post to my server to sign with my private key. That signing request happens through a service proxy class that also uses event based async methods. Once I have the signed request, I add it to the uploader instance and initiate the upload.
I've tried using Concat, but I end up with only the first file going through the process. When I use Merge, all files complete fine, but in a parallel fashion rather than serially. When I use Merge(2) all files start the first step, but then only 2 make their way through and complete.
Obviously I am missing something related to Rx since it isn't behaving like I expect.
namespace RxConcat
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Timers;
public class SignCompletedEventArgs : EventArgs
{
public string SignedRequest { get; set; }
}
public class ChainUploader
{
public IObservable<string> StartUploading(IEnumerable<string> files)
{
return files.Select(
file => from signArgs in this.Sign(file + "_request")
from uploadArgs in this.Upload(file, signArgs.EventArgs.SignedRequest)
select file).Concat();
}
private IObservable<System.Reactive.EventPattern<SignCompletedEventArgs>> Sign(string request)
{
Console.WriteLine("Signing request '" + request + "'");
var signer = new Signer();
var source = Observable.FromEventPattern<SignCompletedEventArgs>(ev => signer.SignCompleted += ev, ev => signer.SignCompleted -= ev);
signer.SignAsync(request);
return source;
}
private IObservable<System.Reactive.EventPattern<EventArgs>> Upload(string file, string signedRequest)
{
Console.WriteLine("Uploading file '" + file + "'");
var uploader = new Uploader();
var source = Observable.FromEventPattern<EventArgs>(ev => uploader.UploadCompleted += ev, ev => uploader.UploadCompleted -= ev);
uploader.UploadAsync(file, signedRequest);
return source;
}
}
public class Signer
{
public event EventHandler<SignCompletedEventArgs> SignCompleted;
public void SignAsync(string request)
{
var timer = new Timer(1000);
timer.Elapsed += (sender, args) =>
{
timer.Stop();
if (this.SignCompleted == null)
{
return;
}
this.SignCompleted(this, new SignCompletedEventArgs { SignedRequest = request + "signed" });
};
timer.Start();
}
}
public class Uploader
{
public event EventHandler<EventArgs> UploadCompleted;
public void UploadAsync(string file, string signedRequest)
{
var timer = new Timer(1000);
timer.Elapsed += (sender, args) =>
{
timer.Stop();
if (this.UploadCompleted == null)
{
return;
}
this.UploadCompleted(this, new EventArgs());
};
timer.Start();
}
}
internal class Program
{
private static void Main(string[] args)
{
var files = new[] { "foo", "bar", "baz" };
var uploader = new ChainUploader();
var token = uploader.StartUploading(files).Subscribe(file => Console.WriteLine("Upload completed for '" + file + "'"));
Console.ReadLine();
}
}
}
The base observable that is handling the 2 step upload for each file is never 'completing' which prevents the next one in the chain from starting. Add a Limit(1) to that observable prior to calling Concat() and it will working correctly.
return files.Select(file => (from signArgs in this.Sign(file + "_request")
from uploadArgs in this.Upload(file, signArgs.EventArgs.SignedRequest)
select file).Take(1)).Concat();

Windows Phone 7 equivalent to NSNotificationCenter?

I'm new to WP7 and coming from iPhone development. On iPhone I'm used to use NSNotificationCenter to notify my program of something. NSNotificationCenter is build-in the framework out of the box. Is there something similar in WP7? I stumbled uppon MVVM-Light Toolkit but I'm not sure how to use it correctly.
What I want to do:
Register to an Notification-Id and do something when Notification-Id is received
Send Notification with Notification-Id and a context (object to pass to observers)
Everyone who registers to the same Notification-Id will be notified
So something like: Registering
NotificationCenter.Default.register(receiver, notification-id, delegate);
Sending:
NotificationCenter.Default.send(notification-id, context);
Example for Registering:
NotificationCenter.Default.register(this, NotifyEnum.SayHello, m => Console.WriteLine("hello world with context: " + m.Context));
Sending ...
NotificationCenter.Default.send(NotifyEnum.SayHello, "stackoverflow context");
Here is how to do with the MVVM Light Toolkit:
Registering:
Messenger.Default.Register<string>(this, NotificationId, m => Console.WriteLine("hello world with context: " + m.Context));
Sending:
Messenger.Default.Send<string>("My message", NotificationId);
Here http://www.silverlightshow.net/items/Implementing-Push-Notifications-in-Windows-Phone-7.aspx you will find a great example on how to use push notification on windows phone 7.
I'm pretty sure that you archive the same result as NSNotificationCenter by creating a singleton which holds a list of observables that implements a specific interface based on your bussiness requirements, or call a lamba, or trigger an event, for each message sent by this singleton you will interate the list of observables and checking the message id, once you find one or more, you can call the interface method, or execute the lambda expression or trigger the event defined to digest the message contents.
Something like below:
public class NotificationCenter {
public static NotificationCenter Default = new NotificationCenter();
private List<KeyValuePair<string, INotifiable>> consumers;
private NotificationCenter () {
consumers = new List<INotifiable>();
}
public void Register(string id, INotifiable consumer) {
consumers.Add(new KeyValuePair(id, consumer));
}
public void Send(String id, object data) {
foreach(KeyValuePair consumer : consumers) {
if(consumer.Key == id)
consumer.Value.Notify(data);
}
}
}
public interface INotifiable {
void Notify(object data);
}
public class ConsumerPage : PhoneApplicationPage, INotifiable {
public ConsumerPage() {
NotificationCenter.Default.Register("event", this);
}
private Notify(object data) {
//do what you want
}
}
public class OtherPage : PhoneApplicationPage {
public OtherPage() {
NotificationCenter.Default.Send("event", "Hello!");
}
}