czyzby/gdx-lml

[WebSocket] get the nvmWebsocket instance

ange-black69 opened this issue · 14 comments

Hello,
I need to get the underlying nvWebSocket instance when I create a websocket via ExtendedNet.createnewWebsocket

I tryed via reflexion to get the instance but it failed.

All I wanted to do is to get the websocket object platform depends to simply change the tcpNoDelay flag on my desktop and android app

Issue related to this : TakahikoKawasaki/nv-websocket-client#141

Thank you.

I don't actively develop my LibGDX utilities lately, but I'll look into before the next release. If this issue blocks you, your best bet is to fork the library and add the method yourself - it's a very simple getter. There's no reason for reflection to fail to fetch the field value either, can you show me the code?

thank you for your quick answer !
Unfortunately I'm not a very good Java Programmer so I don't usually do reflexion all the day.
If you want this is a piece of my code of core :

`

                    socket = ExtendedNet.getNet().newWebSocket(Constants.SERVER_HOST_IP, 2712);
		
		nethandler = new NetworkHandler(l7s,socket);	
		socket.addListener(nethandler);
		l7s.setNethandler(nethandler);
		
		try
		{
			
			socket.connect();
                      



		}
		catch(WebSocketException e)
		{
			e.printStackTrace();
			
		}`

but I found in AndroidLauncher.java, when I call CommonWebSocket.iniate(); it return a new nvWebSocket by a factory, and there I tryed the reflexion but has I don' tknow exactly what to do i'm a bit lost.. I can indeed get the source code and modify by my own but I try to understand exactly how your lib work, I mean how exactly you do the bridge between nvWebSocket and platform WS Implementation

This is the line where a new web socket is created - you probably want to add your extra settings there.

ok thank you, So i just need to clone this github repo as usual and import it as a classic java project, is that right ?

You could do that, but you'll find it easier to create a Java file with the same package and name in your project to patch the library. I remember doing it for some LibGDX classes back when I was starting as well - the compiler/build tool seems to prefer files from your project over the third-party libraries, so as long as you keep the same name and do not modify the publicly used interface, you should be fine.

  • Add com.github.czyzby.websocket.impl package in your source folder.
  • Create NvWebSocket.java file.
  • Copy the source.
  • Apply your changes.

Ok thank you again and again for your help and patience;

I've create package as you described in my android project, added a the new java class into it but I got an error on Line 27 on NvWebSocketLister(this) and when I import it ( import com.github.czyzby.websocket.impl.NvWebSocketListener;) it say that the constructor doesnt accept this type of object :

image

You made a typo. It's czyzby, not czyzboy. This is likely what causes the issue. Packages have to match exactly.

By the way, you should try using IntelliJ/Android Studio instead of Eclipse - especially if you plan on releasing a mobile version.

YEA ! thank you the error disappear !!
Yes i'm using android studio when I want to test on my device the game and deploy a signed apk but I prefer eclipse so I code my core game with eclipse and use android studio only when I debug android and Packaging apk.
Thank you again for your support !

ok this work on desktop.
However on android, it fail on gradle build here the error :

Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/github/czyzby/websocket/impl/NvWebSocket$1; Message{kind=ERROR, text=Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/github/czyzby/websocket/impl/NvWebSocket$1;, sources=[Unknown source file], original message=UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lcom/github/czyzby/websocket/impl/NvWebSocket$1; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334) at com.android.dx.command.dexer.Main.run(Main.java:277) at com.android.dx.command.dexer.Main.main(Main.java:245) at com.android.dx.command.Main.main(Main.java:106) , tool name=Optional.of(Dex)}

That's because both you and gdx-websockets define the same class. I'm pretty sure there's a way to work around this, but I'm afraid Googling com.android.dex.DexException: Multiple dex files define is your best bet for possible solutions.

The alternative is to clone this repo, modify the library source and publish the lib to your Maven Local with gradle build install. Not sure what would require more time to resolve this issue, but you'll have to do some digging on how Gradle/Maven works before getting this right. :)

ok czyzby thank you, I will try this today !

Hello,
I come back with some news.
I think I understand way better how gradle work with maven integration etc..

If i understand correctly, my best bet is to clone this repo, made my change, build the new project into a jar file and then in the build.gradle file compile fileTree(dir: 'libs', include: '*.jar').

I read this usefull article : https://github.com/libgdx/libgdx/wiki/Dependency-management-with-Gradle#mavenizing-local-dependencies
But now i'm a little confused.
I understand that I can publish the lib to my local maven repo, and then from build.gradle script, I can retrieve it localy. ( compile "::"
compile ":::sources" etc..)

So what is the best solution ?

When you clone the repository, you can execute gradle build install to push all libraries to your Maven local. You can also change the version of the dependencies here. Replace libVersion with your custom value and you can import the library in your project as any other artifact.

Make sure to add mavenLocal() to your repositories and then you can just add your custom modified library with:

compile "com.github.czyzby:gdx-websocket-common:1.2.3-YOUR_VERSION"