Giter Club home page Giter Club logo

pun2rx's Introduction

PUN2Rx

Convert PUN 2's callback to UniRx's Operator.

Descriotion

Photon Unity Networking 2 has many callbacks.
Since each event is not independent, it needs to be handled in order.
By treating it as an operator of UniRx, Using PUN2Rx, handling of the event becomes easy.

Also, in order to receive PUN 2 's event callback you need to inherit MonoBehaviourPunCallbacks or inherit each interface.
You can freely change the base class by using PUN2Rx.

PUN2Rx classified them into two types - OnNext only, OnError only.
Most importantly, the normal event operator notifies OnNext only and the error operator notifies OnError only.
With this, it is possible to handle normal events and errors with one Subscribe.
See XMLdoc or table.

WARN: Task isn't supported yet.

Import

Release
download .unitypackage

Requirement

NOTE: Even different versions, it works. Maybe I think...

Example

merge success & failure callback

var successStream = this.OnCreateRoomAsObservable();
var failureStream = this.OnCreateRoomFailedAsObservable();
successStream.Merge(failureStream)
    .Subscribe(
        unit => { Debug.Log("success!"); },
        exception => { Debug.Log("failed..."); }
    );

connect and join, catch error

// room name you want to join
var roomName = "roomName";

// OnNext --- (ConnectedToMaster => OnJoinedRoom)
var successStream =
    this.OnConnectedToMasterAsObservable().Take(1).IgnoreElements()
        .DoOnCompleted(() =>
            {
                Debug.Log("connect finish!");
                Debug.Log("join room!");
                var roomOptions = new RoomOptions();
                PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
            }
        ).Concat(this.OnJoinedRoomAsObservable());


// OnError --- (OnDisconnected or OnJoinRoomFailed)
var failureStream = this.OnDisconnectedAsObservable()
    .Where(cause => cause != DisconnectCause.None && cause != DisconnectCause.DisconnectByClientLogic)
    .Select(cause =>
    {
        throw PUN2Exception.Create((short) cause, cause.ToString());
        return Unit.Default;
    }).Merge(this.OnCreateRoomFailedAsObservable(), this.OnJoinRoomFailedAsObservable());

Observable.Amb(successStream, failureStream)
    .Subscribe(
        unit => { Debug.Log("join room finish!"); },
        exception => { Debug.Log(exception); })
    .AddTo(this);

Debug.Log("connect!");
PhotonNetwork.ConnectUsingSettings();

Operators

Operator Call Type
Photon.Realtime.IConnectionCallbacks - -
OnConnectedAsObservable OnNext Unit
OnConnectedToMasterAsObservable OnNext Unit
OnDisconnectedAsObservable OnNext DisconnectCause
OnRegionListReceivedAsObservable OnNext RegionHandler
OnCustomAuthenticationResponseAsObservable OnNext Tuple<string, object>
OnCustomAuthenticationFailedAsObservable OnError Unit
Photon.Realtime.IInRoomCallbacks - -
OnPlayerEnteredRoomAsObservable OnNext Player
OnPlayerLeftRoomAsObservable OnNext Player
OnRoomPropertiesUpdateAsObservable OnNext HashTable
OnPlayerPropertiesUpdateAsObservable OnNext Tuple<PLayer, Hashtable>
OnMasterClientSwitchedAsObservable OnNext Player
Photon.Realtime.ILobbyCallbacks
OnJoinedLobbyAsObservable OnNext Unit
OnLeftLobbyAsObservable OnNext Unit
OnRoomListUpdateAsObservable OnNext List<RoomInfo>
OnLobbyStatisticsUpdateAsObservable OnNext List<TypedLobbyInfo>
Photon.Realtime.IMatchmakingCallbacks
OnFriendListUpdateAsObservable OnNext List<FriendInfo>
OnCreateRoomAsObservable OnNext Unit
OnCreateRoomFailedAsObservable OnError Unit
OnJoinedRoomAsObservable OnNext Unit
OnJoinRoomFailedAsObservable OnError Unit
OnJoinRandomFailedAsObservable OnError Unit
OnLeftRoomAsObservable OnNext Unit
Photon.Pun.IPunOwnershipCallbacks
OnOwnershipRequestAsObservable OnNext Tuple<PhotonView, Player>
OnOwnershipTransferredAsObservable OnNext Tuple<PhotonView, Player>

Author

nekomimi-daimao

LICENCE

MIT

pun2rx's People

Contributors

nekomimi-daimao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

r01hee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.