Mazyod / PhoenixSharp

C# Phoenix Channels client. Unity Compatible.
MIT License
163 stars 27 forks source link

Help reading message from callbacks #5

Closed jonathanleang closed 7 years ago

jonathanleang commented 7 years ago

I am new to callbacks in unity, not sure how to read the message and replies. I did the following and got NULL as expected.

void JoinLobby(Socket socket){
    Reply? okReply = null;
    Reply? errorReply = null;
    Message afterJoinMessage = null;
    Message closeMessage = null;
    Message errorMessage = null;

    var roomChannel = socket.MakeChannel("room:lobby");
    roomChannel.On(Message.InBoundEvent.phx_close, m => closeMessage = m);
    roomChannel.On("after_join", m => afterJoinMessage = m);

    roomChannel.Join()
        .Receive(Reply.Status.Ok, r => okReply = r)
        .Receive(Reply.Status.Error, r => errorReply = r);

    Debug.Log (okReply);
    Debug.Log (errorReply);
    Debug.Log (afterJoinMessage);

}
Mazyod commented 7 years ago

@jonathanleang First you need a phoenix server running, with some available channels. Once that's up and running, then you can start writing client code to connect to the server.

jonathanleang commented 7 years ago

@Mazyod yeh i already have that setup and working with the js client I am just not sure how to read Reply and Message, they are still null

jonathanleang commented 7 years ago

its working now, thanks

def connect(_params, socket) do {:ok, assign(socket, :user, :guest)} end

roomChannel.Join() .Receive(Reply.Status.Ok, r => okReply(r))

public void okReply(Reply r){ Debug.Log ("okReply" + r); }

Mazyod commented 7 years ago

Cool, you can also simply pass in the function:

roomChannel.Join()
    .Receive(Reply.Status.Ok, okReply);
jonathanleang commented 7 years ago

Thanks for the tips

jonathanleang commented 7 years ago

@Mazyod Thanks and I got it working in unity editor, sending and reading messages, but when it's on iOS i got the following error.

2017-06-16 22:28:57.151455+0800 ProductName[6120:1690677] Warning: Libinfo call to mDNSResponder on main thread [Debug]: socket - on open 06/16/2017 22:28:58|Fatal|Socket.Connect|System.MissingMethodException: Method not found: 'Default constructor not found...ctor() of System.ComponentModel.TypeConverter'. at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in :0 at System.ComponentModel.TypeDescriptor.GetConverter (System.Type type) [0x00000] in :0 at Newtonsoft.Json.Serialization.DefaultContractResolver.CanConvertToString (System.Type type) [0x00000] in :0 at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in :0 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in :0 at Newtonsoft.Json.Linq.JToken.FromObjectInternal (System.Object o, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00000] in :0 at Newtonsoft.Json.Linq.JObject.FromObject (System.Object o, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00000] in :0 at Phoenix.MessageSerialization.Serialize (Phoenix.Message message) [0x00000] in :0 at Phoenix.Socket.Push (Phoenix.Message msg) [0x00000] in :0 at Phoenix.Socket.SendHeartbeat () [0x00000] in :0 at Phoenix.Socket.WebsocketOnOpen (IWebsocket ws) [0x00000] in :0 at WebSocketSharp.WebSocket.open () [0x00000] in :0 at Phoenix.Socket.Connect (System.String url, System.Collections.Generic.Dictionary2 parameters) [0x00000] in <filename unknown>:0 at ServerConnect+<ReConnect>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00000] in <filename unknown>:0 [Info]: socket - An exception has occurred while opening. [Info]: channel - room:lobby: socket error onErrorCallback An exception has occurred while opening. ServerConnect:onErrorCallback(String) Phoenix.Socket:WebsocketOnError(IWebsocket, String) WebSocketSharp.WebSocket:acceptException(Exception, String) Phoenix.Socket:Connect(String, Dictionary2)

c__Iterator0:MoveNext() UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
jonathanleang commented 7 years ago

socket.Connect(string.Format("{0}://{1}/socket", wsprotocol, host), new Dictionary<string, string> { { "token", token } }); lobbyChannel = socket.MakeChannel("room:lobby");

Mazyod commented 7 years ago

@jonathanleang I would suggest you try the Json.NET library from the asset store. The libraries I've added in this repo (WebsocketSharp and NewtonSoft) are unfortunately not fully compatible with iOS.

Feel free to try and search for free alternatives, but from my experience, Json.NET was worth every penny.

Mazyod commented 7 years ago

Further reading: https://github.com/Mazyod/PhoenixSharp#unity

jonathanleang commented 7 years ago

@Mazyod Thanks a lot, your response is so quick !

Mazyod commented 7 years ago

Happy to help! Thanks for giving this a try, and please let me know of any issues

jonathanleang commented 7 years ago

@Mazyod Yep, its working on ios now with Json.NET, thank you.

jonathanleang commented 7 years ago

@Mazyod any idea why 'Phoenix.Push' is missing ? This happend after i changed to BestHTTP Pro

TypeLoadException: Could not load type 'Phoenix.Push' from assembly 'Phoenix, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. System.Reflection.MonoMethodInfo.GetMethodInfo (IntPtr handle) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:59) System.Reflection.MonoMethodInfo.GetAttributes (IntPtr handle) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:75) System.Reflection.MonoMethod.get_Attributes () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:245) System.Reflection.MethodBase.get_IsSpecialName () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:184) UnityEditor.Build.BuildPipelineInterfaces.InitializeBuildCallbacks (Boolean findBuildProcessors, Boolean findSceneProcessors) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildPipelineInterfaces.cs:149)

Mazyod commented 7 years ago

@jonathanleang how are you adding PhoenixSharp code to your Unity project? Are you building it as a DLL, then linking that?

Personally, I have faced a lot of issues compiling DLLs separately in the past with Unity, and simply drop the sources directly into the Unity project.

jonathanleang commented 7 years ago

@Mazyod thanks, fixed it by removing DLL, I only used unity to build not sure how debug DLL got there. Thanks alot, everything looks good now 👍