Mazyod / PhoenixSharp

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

Better Guides #21

Open Mazyod opened 2 years ago

Mazyod commented 2 years ago

I think the library is missing detailed guides to help developers more easily integrate the library with their project. Need to fix that.

ekrenzin commented 2 years ago

@Mazyod I'm attempting to integrate the library and running into some trouble figuring it out. Is there any way you could help me resolve some of the issues I'm having with integrating this into my Unity project?

I attempted to build a dll, but I get an assembly error: image

When I attempt to import the source code manually I run into other assembly reference errors: image

I believe I've properly set my project to .NET 4.x image

I'm assuming there is some small but essential point that I missed during the setup. I was able to successfully run the unit tests.

Thanks in advance @Mazyod I really appreciate the work you have done on this repo!

Mazyod commented 2 years ago

Hey @ekrenzin 👋, Sure, I'd be happy to help!

I'm going to try importing the code into a fresh project with the settings you've shared. Meanwhile, could you please share the Unity version you're using? Perhaps also the Target platform would be helpful, although I'm guessing it's macOS.

ekrenzin commented 2 years ago

Hey @ekrenzin 👋, Sure, I'd be happy to help!

I'm going to try importing the code into a fresh project with the settings you've shared. Meanwhile, could you please share the Unity version you're using? Perhaps also the Target platform would be helpful, although I'm guessing it's macOS.

@Mazyod Thanks for the fast response! I'm building a VR application, so the target platform is windows & android. I created a fresh 3d project to test/confirm that I hadn't accidentally changed a setting, but I ended up with the same results. To be transparent, I'm quite new to unity and c#. It's not unlikely that there is a basic essential concept that I am unaware of.

I'm working on a windows 11 machine - but I could switch to either windows 10 or a m1 mac if that would make a difference. I'm using Rider as an IDE.

For importing the source code, the most confusing error to me is GetValueOrDefault not recognized. However the same code GetValueOrDefault is recognized when opening & building PhoenixSharp separately from my unity projects. Is it possible that I'm missing a unity setting beyond changing to NET 4.x?

Similarly, when adding the dll as a plugin it almost seems like System.Runtime.CompilerServices.TaskAwaiter is missing? image

Error Message:
An exception has occurred during the OnClose event.
UnityEngine.Debug:Log (object)
PhoenixTests.WebSocketImpl.WebsocketSharpAdapter:OnWebsocketError (object,WebSocketSharp.ErrorEventArgs) (at Assets/Scripts/WebSocketSharp.cs:66)
WebSocketSharp.Ext:Emit<WebSocketSharp.ErrorEventArgs> (System.EventHandler1<WebSocketSharp.ErrorEventArgs>,object,WebSocketSharp.ErrorEventArgs)
Error Exception: 
System.TypeLoadException: Could not resolve type with token 0100001c (from typeref, class/assembly System.Runtime.CompilerServices.TaskAwaiter, System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
  at Phoenix.Scheduler.ScheduleTimeout () [0x00039] in <76c0f2e7127d47e58aa61f16f390a4ca>:0 
  at Phoenix.Socket.OnConnClose (Phoenix.IWebsocket websocket, System.UInt16 code, System.String reason) [0x00069] in <76c0f2e7127d47e58aa61f16f390a4ca>:0 
  at PhoenixTests.WebSocketImpl.WebsocketSharpAdapter.OnWebsocketClose (System.Object sender, WebSocketSharp.CloseEventArgs args) [0x00000] in

Plugins: image

Here is the build logs from building PhoenixSharp with Rider: (2 warnings but a dll is created) image image

Target Platform: (windows) image

Editor Version: (2020.3.33f1) image

Let me know if there is any other information I can give that might be helpful.

Thanks again!

Mazyod commented 2 years ago

I should’ve guessed Windows from the file paths 😅 .. It’s great that you are willing to share your experience! With so much details too.

I’ve created this issue about docs specifically because others had trouble integrating the library, but only mentioned it on social media without details.

So .. Regarding the issue, I will have time later today. I will look into the stuff you shared in detail, and prepare a windows environment from my side. You definitely shouldn’t have to switch OSs, as this library’s main goal is portability!

Mazyod commented 2 years ago

Hey @ekrenzin, I pushed an update that I hope will solve your issue. I didn't have time to test it, just confirmed it compiles in the following environment:

Let me know how it goes, and if there is anything else that needs attention. I plan to test it more once I get the time.

What Changed?

I've been testing the library with Unity 2022.x, which reached LTS status just weeks back, and it has better support for the latest C# features and API compatibility. It is not reasonable to ask users to upgrade in order to use the library, so using higher compatibility API (lower standard version) was the way to go.

The project now is based on the .NET Standard 2.0, which should be highly compatible with the largest number of platforms and Unity versions. All APIs have been accommodated to that standard level.

What about .NET 4.x?

According to the Unity docs, .NET 4.x should be even more compatible than .NET Standard 2.0, however, I couldn't get it to work locally. I believe it has to do with the csc.rsp file to link additional assemblies used in the code.

I didn't bother research it in detail, because users should use .NET Standard 2.0 when possible, and if they needed .NET 4.x, it means they would probably know how to link the additional assemblies. (This is just an assumption from my side)

ekrenzin commented 2 years ago

@Mazyod Thank you so much for this update!

I was finally able to get a socket open, and I'm so excited! image

I really appreciate how fast you implemented the compatibility changes, you are a hero!

Rotcline commented 1 year ago

Hi Mazyod, I want to implement your library on my Unity project but I don't know how in the past days I tried to do it by following the readme but I think I am missing something. I have worked previously with Phoenix Channels on a React Project but right now I don't know what I am doing. I am following this steps:

  1. Download the repo
  2. Put everything into my assets folder on Unity
  3. Install NUnit, WebSocketSharp and Newtonsoft.Json
  4. Try to connect to my socket Would you help me by telling me what step I am missing? I am working on a Mac M1 with Mono .Net 2.1 in Rider

Edit: I can connect to the socket now but this test of the integration tests file is not passing imagen

Mazyod commented 1 year ago

@Rotcline Hey 👋 Sorry for the late reply. The server for integration tests was down, but now it's up again. I suspect that's the reason for the test failure.

Rotcline commented 1 year ago

@Mazyod thank you I had trouble with the tests but now is working just fine with my own server

ChristopherKRad commented 1 year ago

@Mazyod Hello! I'm having some difficulty installing the repo into a blank Unity project. I'm a complete newbie when it comes to Unity but have some experience with Elixir and Phoenix Live View. As @Rotcline outlined the steps earlier, I'm a bit unclear as to how to install the dependencies and have been getting a ton of different errors when trying to install the project and haven't yet got to implement my own IWebsocketFactory.

Below is the version of Unity and the approach I have used to install the deps:

Version of Unity 2020.3.15f2

  1. Newtonsoft.Json

    • Installed via git URL
    • Window > Package Manager > Add Package from git URL > com.unity.nuget.newtonsoft-json
  2. WebSocketSharp

    • Added by downloading the latest master branch zip to my Assets directory
  3. NUnit

    • Installed via git URL
    • Window > Package Manager > Add Package from git URL > com.unity.ext.nunit@1.0
  4. PhoenixSharp

    • Added by downloading the latest master branch zip to my Assets directory
Mazyod commented 1 year ago

Sorry about the suboptimal experience with integrating the library, @ChristopherKRad. Happy to help you through this.

I might guess that the error dump you're seeing is probably due to a misconfiguration and/or unnecessary files included in the project. Can you please share a few errors for me to assess?

Otherwise, here is what I did to get the project working on the Unity 2020.3.47 release:

  1. Created a new project
  2. Created a folder "Phoenix" to hold the library files
  3. Added the *.cs files only to that folder.
  4. Added NewtonSoft.json package only using the same approach you did.

Although, I noticed NUnit being there in the project, without me adding it explicitly somehow.

image
ChristopherKRad commented 1 year ago

I appreciate the swift response @Mazyod :) It's not suboptimal, it's likely due to my lack of familiarity with Unity.

I have a phoenix live view project which is printing "{url: www.example.com}" every 5 seconds and is deployed via fly.io

I've created a sample repo to show what my Assets directory looks like (assume I have the Newtonsoft dependency loaded via the package manager, and am using the example WebSocketSharp and JsonMessageSerializer provided).

Based on my limited understanding, the message should print in the unity console when I hit play, but maybe there's something missing?

I'd appreciate you taking a look at the repo I've created below, and letting me know if there's something I need to change on PhoenixLiveViewConnector.cs

https://github.com/ChristopherKRad/examplePhoenixSharp

Mazyod commented 1 year ago

@ChristopherKRad Also, make sure that you rename the main MonoBehavior script to WebSocketClient.cs then add the script to a game object in the scene (e.g. Main Camera). This will make the script actually run.

I did that then was seeing error logs popup in the console. The errors were related to the set up and communicating with Phoenix LiveView perhaps, so you might have better luck there.