robertdmunn/websockets

Need examples

Opened this issue · 1 comments

Can you include a simple example of sending and receiving messages?

I use Eclipse to build the package (I use Export -> Java -> jar to build the jar file ), but you can use any Java IDE or the CLI as long as the compiler can find the imported classes it needs:

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_10;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

So you need to make the org.java_websocket classes available so the compiler can find them.

As for how to instantiate the object and make calls without Javaloader, you need to copy the jar file and the org.java_websocket jar file to the custom Java classes folder in your Lucee installation so Lucee can see the classes. I don’t know what that folder is offhand in your Lucee install, should be in the docs or you can ask on the Lucee mailing list. It might be in your web context under /WEB-INF/lucee/lib but I can’t verify that folder is the correct one. Any reason not to use Javaloader? It has a lot of advantages over loading classes without it.

Here is a way to instantiate with Javaloader:

var paths = arraynew(1);
paths[1]= expandpath("/includes/java/java_websocket.jar");
paths[2]= expandpath("/includes/java/com.bonnydoonmedia.io.jar");
var uri = createObject( "java", "java.net.URI" ).init( "ws://" & getProperty("host") & ":" & getProperty("port") );
instance.javaloader = createObject('component',"javaloader.JavaLoader").init(paths);
instance.ws = instance.javaloader.create( "com.bonnydoonmedia.io.WSClient" ).init( uri );

without Javaloader:

var uri = createObject( "java", "java.net.URI" ).init( "ws://" & hostname & ":" & port );
instance.ws = createObject( “java”, "com.bonnydoonmedia.io.WSClient" ).init( uri );

Once you have the ws object, you need to connect to the endpoint:

instance.ws.connect();

Then you create an entry (a string ) and send it:

entry = "#severityToString(loge.getseverity())# #loge.getCategory()# #loge.getmessage()# ExtraInfo: #loge.getextraInfoAsString()#";

and send it:

instance.ws.send( entry );

This code is from a LogBox logging appender so you notice some formatting and objects ( loge ) native to LogBox, but the idea is the same. Note that you also need to do

instance.ws.disconnect

when the application ends to exit cleanly.

That’s pretty much it.

HTH

Robert

On Sep 18, 2016, at 12:48 PM, JP notifications@github.com wrote:

Can you include a simple example of sending and receiving messages?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub #2, or mute the thread https://github.com/notifications/unsubscribe-auth/AE9sOu31BQXX8wdT4AKkuSQ2wnRCpjToks5qrWtBgaJpZM4J_8re.