doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
729 stars 125 forks source link

System.Text.Json Reference Problem #259

Closed DavidGameDev closed 2 years ago

DavidGameDev commented 2 years ago

Due to some platform limitations I cannot use the System.Text.Json and instead I would use the Newtonsoft Json as per the specifications. However, the System.Text.Json is still referenced in the dll and thus I cannot compile the software. Is there any version or solution I could use where the System.Text.Json Reference is replaced with the Newtonsoft Json?

doghappy commented 2 years ago

have you tried this?

install SocketIOClient.Newtonsoft.Json from NuGet

var client = new SocketIO("http://localhost:11000/");
var jsonSerializer = new NewtonsoftJsonSerializer();
/*
jsonSerializer.OptionsProvider = () => new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy()
    }
};
*/
client.JsonSerializer = jsonSerializer;
DavidGameDev commented 2 years ago

Yes, the problem is just the System.Text.Json Library reference in the dll. When building the project it always outputs a System.Text.Json error, even if I replace the Serializer with NewtonsoftJson in code.

doghappy commented 2 years ago

What is the error and on what platform?

DavidGameDev commented 2 years ago

I am using it with Unity and building it for WebGL il2cpp Platform.

I get the following error which is due to the mentioned Json library.

Building Library\Bee\artifacts\WebGL\GameAssembly.\master_WebGL_wasm\zqe2_Text.Json1.o failed with output:
D:\Projects\Unity\SocketIOTest\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\System.Text.Json1.cpp:12360:11: error: no viable overloaded '='
V_3 = L_12;
~~~ ^ ~~~~
D:\Projects\Unity\SocketIOTest\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\System.Text.Json1.cpp:2612:8: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'ValueTask_t10B4B5DDF5C582607D0E634FA912F8CB94FCD49F' to 'const ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711' for 1st argument
struct ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711
^
D:\Projects\Unity\SocketIOTest\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\System.Text.Json1.cpp:2612:8: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'ValueTask_t10B4B5DDF5C582607D0E634FA912F8CB94FCD49F' to 'ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711' for 1st argument
struct ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711
^
D:\Projects\Unity\SocketIOTest\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\System.Text.Json1.cpp:12884:9: error: no viable overloaded '='
V_4 = L_8;
~~~ ^ ~~~
D:\Projects\Unity\SocketIOTest\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\System.Text.Json1.cpp:2612:8: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'ValueTask_t10B4B5DDF5C582607D0E634FA912F8CB94FCD49F' to 'const ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711' for 1st argument
struct ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711
^
D:\Projects\Unity\SocketIOTest\Library\Bee\artifacts\WebGL\il2cppOutput\cpp\System.Text.Json1.cpp:2612:8: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'ValueTask_t10B4B5DDF5C582607D0E634FA912F8CB94FCD49F' to 'ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711' for 1st argument
struct ValueTask_tD1A250CC105307FE5FB0D9D930345BB719E6B711
^
2 errors generated.
emcc2: error: '"C:/Program Files/Unity/Hub/Editor/2021.2.7f1/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/Emscripten/llvm\clang++.exe" -target wasm32-unknown-emscripten -DEMSCRIPTEN -fno-inline-functions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_major__=2 -D__EMSCRIPTEN_minor__=0 -D__EMSCRIPTEN_tiny__=19 -D_LIBCPP_ABI_VERSION=2 -Dunix -D__unix -D__unix__ -Werror=implicit-function-declaration -Xclang -iwithsysroot/include/SDL "--sysroot=C:\Program Files\Unity\Hub\Editor\2021.2.7f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\emscripten\cache\sysroot" -Xclang -iwithsysroot/include\compat -D__webgl__ -Wno-c++11-extensions -Wno-nonportable-include-path -ffunction-sections -fno-unwind-tables -fomit-frame-pointer -fno-threadsafe-statics -std=c++11 -Wno-#warnings -Wswitch -Wno-trigraphs -Wno-tautological-compare -Wno-invalid-offsetof -Wno-implicitly-unsigned-literal -Wno-integer-overflow -Wno-shift-negative-value -Wno-unknown-attributes -Wno-implicit-function-declaration -Wno-null-conversion -Wno-missing-declarations -Wno-unused-value -Wno-pragma-once-outside-header -fvisibility=hidden -fexceptions -fno-rtti -fno-strict-overflow -ffunction-sections -fdata-sections -fmessage-length=0 -pipe -DBASELIB_INLINE_NAMESPACE=il2cpp_baselib -DIL2CPP_MONO_DEBUGGER_DISABLED -DRUNTIME_IL2CPP -DHAVE_BDWGC_GC -DNDEBUG -I. -ID:/Projects/Unity/SocketIOTest/Library/Bee/artifacts/WebGL/il2cppOutput/cpp "-IC:/Program Files/Unity/Hub/Editor/2021.2.7f1/Editor/Data/il2cpp/libil2cpp/pch" "-IC:/Program Files/Unity/Hub/Editor/2021.2.7f1/Editor/Data/il2cpp/libil2cpp" "-IC:/Program Files/Unity/Hub/Editor/2021.2.7f1/Editor/Data/il2cpp/external/baselib/Include" "-IC:/Program Files/Unity/Hub/Editor/2021.2.7f1/Editor/Data/il2cpp/external/baselib/Platforms/WebGL/Include" -Oz -fcolor-diagnostics -fdiagnostics-absolute-paths -UGC_THREADS -UUSE_MMAP -UUSE_MUNMAP -c -xc++ D:/Projects/Unity/SocketIOTest/Library/Bee/artifacts/WebGL/il2cppOutput/cpp/System.Text.Json1.cpp -o Library/Bee/artifacts/WebGL/GameAssembly./master_WebGL_wasm/zqe2_Text.Json1.o' failed (1)
UnityEngine.GUIUtility: ProcessEvent (int,intptr,bool&)
doghappy commented 2 years ago

I will create a new branch, using NewtonsoftJson instead of SystemTextJson. Do you think this will help you?

DavidGameDev commented 2 years ago

Yes, that should be very helpful. It will also be probably very helpful to any other Unity Dev that would like to use this library especially for Web Builds. Even if it doesn't work from the get go, I can then just rebuild the source with the Unity NewtonsoftJson package, which then should be workable with. But the latest v13+ of Newtonsoft Json should work perfectly fine with Unity. Thank you very much!

For any other Unity Devs who are interesseted, the Unity Newtonsoft Json package is https://github.com/jilleJr/Newtonsoft.Json-for-Unity

doghappy commented 2 years ago

https://github.com/doghappy/socket.io-client-csharp/tree/newtonsoft-json

I've removed System.Text.Json and replaced it with Newtonsoft.Json.

DavidGameDev commented 2 years ago

Hey, thank you very much. It definitelly helped. When used with the Unity Newtonsoft Json Library it works! (I still have a Unity related build Issue, but the System.Text.Json Problem is definitelly fixed!) Thank you again, very much appreciated!

Snowblaze commented 2 years ago

Hey @DavidGameDev, how did you manage to make it work? I built the Newtonsoft project from the solution, imported sockeio and socketio.newtonsoft to the plugins folder in my project and it seemed to work in the editor. But when I try to build the project for desktop IL2CPP, it throws a linking error. Although I am using newtonsoft from the solution rather than using the jillejr pacakge in unity package manager. Any pointers would be very much appreciated!

DavidGameDev commented 2 years ago

@Snowblaze Do you mean for WebGL with IL2CPP? I also did build it with the Newtonsoft from the solution. If I remember correctly, I think had to create a link.xml (you will find some references to it when you google it or search for that in the unity forums). Can you post what the specific linking error is? However, I must disappoint you, I had to scrap the Unity Project due to time constraints. The problem was, even after being able to build the game to IL2CPP WebGL, it didn’t work, as Unity’s WebGL Builds seem to have a problem with async and awaits, there was a Ticket (https://issuetracker.unity3d.com/issues/webgl-async-tasks-do-not-run-on-webgl-when-threading-is-enabled) about this, Unity marked it as resolved, however, from comments and my experience, the problem persists and the communication with socket.io backend does not work. It doesn’t send nor receive anything.

Snowblaze commented 2 years ago

@DavidGameDev My build target is desktop (Windows). IL2CPP fails to build, and when I change to Mono it succeeds, but as I run the game, it opens up and nothing works as the assembly is missing as far as I can tell. Although when I run the game in the Editor it both connects to my server and emits/receives all events. I suppose it is missing in your build as well, if it works in the Editor for you.

Here are the errors that I get. #267

DavidGameDev commented 2 years ago

@Snowblaze I remember what I did. Yeah, it was a problem with the Newtonsoft Package. I did build it with the Newtonsoft, however, in Unity, I installed the jilleJr Json.NET 13.0.1 for Unity Package, had to uninstall the i believe it was the Unity "Version Control" package and then it worked for me. I hope this might help. But as I said, I got to build it for WebGL with IL2CPP, but it didnt work. Due to project constraints, we had to ditch Unity and we work with something else, so I never got to a final solution that works.

Snowblaze commented 2 years ago

@DavidGameDev I really appreciate your help. Installed the jilleJr package and it worked like a charm. Built the app successfully! Cheers!