This is a simple example of a web socket based communication between the server and the clients, exchanging simple string messages. Note that it does not come packed with Gradle wrapper, so make sure to install it on your own.
Client uses LibGDX (obviously) along with gdx-websocket
library. Desktop uses gdx-websocket-common
natives library, while GWT project depends on gdx-websocket-gwt
. Server is built with a few lines of code thanks to the amazing Vert.x framework.
The server should listen to web socket connections on 8000
port, send a simple string message after each successful connection and disconnect every user after 5 seconds. Clients should connect with the server, send a simple message after successful connection and keep on printing web socket status on the screen until disconnected. All expected events are logged to the platform-specific console.
Server can be run using ServerLauncher
class in the server
project; desktop client uses standard DesktopLauncher
class, partially generated by the gdx-setup
app in the desktop
project.
You can also check out application's behavior without an IDE thanks to Gradle tasks:
gradle server:run
launches the server.gradle desktop:run
launches desktop client. Note thatgdx-websocket-common
library should also work on Android (hence the name), so Android code and dependencies would be similar.gradle html:superDev
compiles GWT application and provides the web client version on8080
port. Visithttp://localhost:8080
to check it out.