ServerSocket from within Tapestry

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

ServerSocket from within Tapestry

Christopher
Hi team,

I'm in the final stages of developing a Tapestry webapp for a
multi-contractor worksite.  Most user interactions will be via webpage
forms, with state displayed on a dashboard page, involving mostly text
(though done attractively).

To support this application I've also developed a small desktop
application to reflect state in a more graphical way, using Java 2D
graphics.  Communication between server and client would be socket-based
transmission of objects: ObjectOutputStream + ObjectInputStream.

My question is how best to implement and launch this serverside Java
networking code from inside a Tapestry application.  Ideally I'd like to
generate data for a client using CrudServiceDAO, in much the same way as I
would generate an HTTP response within a Tapestry page.  The server code
should start on launching the Tapestry application, and immediately start
listening on a given port.

Any suggestions would be much appreciated.

Regards,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry.
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Eugen
Hi Christopher,
to do somewhat similar I`m using Tomcat WebSockets (the Software are
deployed allways on Tomcat Server).
The Desktop application communicates over Java WS Client, WebPages over
JavaScript implementation. Transmitted Data is serialized into JSON Strings.
On Server-Side the @ServerEndpoint is a Tapestry Service what implements
specific WebSocket Functions (I'm using annotations, e.g. @OnMessage etc)

The ws Path should be added to IgnorePathFilter in AppModule:

public static void contributeIgnoredPathsFilter(Configuration<String>
configuration) {
   configuration.add("/ws");
}

Regards
Eugen

2018-06-06 14:30 GMT+02:00 Christopher Dodunski <
[hidden email]>:

> Hi team,
>
> I'm in the final stages of developing a Tapestry webapp for a
> multi-contractor worksite.  Most user interactions will be via webpage
> forms, with state displayed on a dashboard page, involving mostly text
> (though done attractively).
>
> To support this application I've also developed a small desktop
> application to reflect state in a more graphical way, using Java 2D
> graphics.  Communication between server and client would be socket-based
> transmission of objects: ObjectOutputStream + ObjectInputStream.
>
> My question is how best to implement and launch this serverside Java
> networking code from inside a Tapestry application.  Ideally I'd like to
> generate data for a client using CrudServiceDAO, in much the same way as I
> would generate an HTTP response within a Tapestry page.  The server code
> should start on launching the Tapestry application, and immediately start
> listening on a given port.
>
> Any suggestions would be much appreciated.
>
> Regards,
>
> Chris.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Christopher
In reply to this post by Christopher
Thanks Eugen, and where did you place the server-side class decorated with
@ServerEndpoint("/ws")?  Perhaps it doesn't matter whether it is placed in
'pages' or 'services', as Tomcat finds it regardless.

Is it necessary to invoke new threads (multi-threading) within each of the
methods decorated with @OnOpen, @OnClose, @OnMessage and @OnError, or does
Tomcat automatically handle cases of multiple, simultaneous client
connections?

I found the below 'howto' on using Tomcat WebSockets, but the client side
code is implemented in HTML and JavaScript, not Java.  It would be useful
to find some sample, client-side Java code for connecting and
communicating via a Tomcat WebSocket.

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-websocket-tutorial/

Regards,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry.
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Eugen
Hi Christopher,
In my case the annotation @ServerEndpoint decorates an Service
implementation class.

I don't invoke any threads in WS Methods, but, i think You can use the
Tapestry's ParralelExecutor Service to optimize the processing of client
requests (see https://tapestry.apache.org/parallel-execution.html)

As WebSocket Client I use this library -
https://github.com/TooTallNate/Java-WebSocket

Regards
Eugen


2018-06-07 8:10 GMT+02:00 Christopher Dodunski <
[hidden email]>:

> Thanks Eugen, and where did you place the server-side class decorated with
> @ServerEndpoint("/ws")?  Perhaps it doesn't matter whether it is placed in
> 'pages' or 'services', as Tomcat finds it regardless.
>
> Is it necessary to invoke new threads (multi-threading) within each of the
> methods decorated with @OnOpen, @OnClose, @OnMessage and @OnError, or does
> Tomcat automatically handle cases of multiple, simultaneous client
> connections?
>
> I found the below 'howto' on using Tomcat WebSockets, but the client side
> code is implemented in HTML and JavaScript, not Java.  It would be useful
> to find some sample, client-side Java code for connecting and
> communicating via a Tomcat WebSocket.
>
> https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-
> websocket-tutorial/
>
> Regards,
>
> Chris.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Carlos Montero Canabal
In reply to this post by Christopher
Hi!

I have to develop a chat on a Tapestry5 app and I have to include WebSockets. To do it i put the config below:

I create a ChatServlet class outside Tapestry5 packages

my.package.
                    chat -> ChatServlet
                    web.
                           pages
                           components
                           services
                           mixins

ChatServlet.java :

package mypackage.chat;

@ServerEndpoint(value = "/chat")
public class ChatServlet extends HttpServlet {
       
        @OnOpen
        public void onOpen(Session session) throws Exception {
        …

        @OnMessage
        public void onMessage(Session session, String message) throws Exception {
        …

AppModule.java:

public static void contributeIgnoredPathsFilter(final Configuration<String> configuration) {
                configuration.add("/chat.*");
}

web.xml

        <servlet>
                <servlet-name>chat-servlet</servlet-name>
                <servlet-class>mypackage.chat.ChatServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
                <servlet-name>chat-servlet</servlet-name>
                <url-pattern>/chat</url-pattern>
        </servlet-mapping>

And after to add this config, you can connect directly. With javascript is:

new WebSocket("ws://"+ baseUrl +"/chat”);

I hope the configuration for webapp helps you.

Regards

Carlos Montero Canabal


> El 7/6/2018, a las 8:10, Christopher Dodunski <[hidden email]> escribió:
>
> Thanks Eugen, and where did you place the server-side class decorated with
> @ServerEndpoint("/ws")?  Perhaps it doesn't matter whether it is placed in
> 'pages' or 'services', as Tomcat finds it regardless.
>
> Is it necessary to invoke new threads (multi-threading) within each of the
> methods decorated with @OnOpen, @OnClose, @OnMessage and @OnError, or does
> Tomcat automatically handle cases of multiple, simultaneous client
> connections?
>
> I found the below 'howto' on using Tomcat WebSockets, but the client side
> code is implemented in HTML and JavaScript, not Java.  It would be useful
> to find some sample, client-side Java code for connecting and
> communicating via a Tomcat WebSocket.
>
> https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-websocket-tutorial/
>
> Regards,
>
> Chris.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Christopher
In reply to this post by Christopher
Hi Eugen,

I like the look of the WebSocket library you shared:
https://github.com/TooTallNate/Java-WebSocket.  It is 100% Java, and
includes example code for both server and client.  Also it supports
multi-threading 'out of the box'.

So, if inside my Tapestry project's 'services' directory I create my own
WebSocket server, extending the abstract server class included in the
above library, it seems I can PUSH data out from the server simply by
calling this service whenever a user makes a change using the Tapestry
webapp.  This way any connected desktop app clients always reflect current
state.  Have I understood correctly?

Another small question... the demo server class in the above library
includes a main method with the below lines of code to start up the
server:

  WebSocketServer server = new SimpleServer(new InetSocketAddress(host,
port));
  server.run();

How do I start up this WebSocket server in the services directory of my
Tapestry project?

Thanks,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry.
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Christopher
In reply to this post by Christopher
Hi Carlos,

Thanks for detailing how you implemented a WebSocket using an external
HttpServlet.  I'm sure it works well for you.  However, since it seems
possible to implement a WebSocket right inside a Tapestry app, I will
attempt to do it this way first.  It keeps everything self-contained and
tidy, which has its benefits.

Kind regards,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry.
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Christopher
In reply to this post by Christopher
I like the look of the WebSocket library below:

https://github.com/TooTallNate/Java-WebSocket

It is 100% Java, and includes example code for both server and client.
Also, it supports multi-threading 'out of the box'.

If I were to use this library's server code, placed in the Tapestry
'services' directory, must I still decorate the server class with
@ServerEndPoint for Tomcat/Tapestry to direct WebSocket connections to it?

Also, where would you recommend placing the below lines of start up code
in order to run the WebSocket server inside the 'services' directory?

  WebSocketServer server = new SimpleServer(new InetSocketAddress(host,
port));
  server.run();

Thanks,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry.
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Eugen
Hi Christopher,
By decorating a Tapestry Service by @ServerEndPoint You can use all
Tapestry benefits like Injection etc.
You don't need to start any additional servers.. the application is already
deployed on Tomcat and Tomcat will serve classes decorated by
@ServerEndPoint. Important is to exclude the WS Pats from Tapestry's filter.
The code avove is usefull when You use the server standalone (without
Tomcat). Ffrom the Java-Websocket Docs: A WebSocket server by itself
doesn't do anything except establish socket connections though HTTP. After
that it's up to *your* subclass to add purpose.

kind regards
Eugen

пн, 11 июн. 2018 г. в 13:45, Christopher Dodunski <
[hidden email]>:

> I like the look of the WebSocket library below:
>
> https://github.com/TooTallNate/Java-WebSocket
>
> It is 100% Java, and includes example code for both server and client.
> Also, it supports multi-threading 'out of the box'.
>
> If I were to use this library's server code, placed in the Tapestry
> 'services' directory, must I still decorate the server class with
> @ServerEndPoint for Tomcat/Tapestry to direct WebSocket connections to it?
>
> Also, where would you recommend placing the below lines of start up code
> in order to run the WebSocket server inside the 'services' directory?
>
>   WebSocketServer server = new SimpleServer(new InetSocketAddress(host,
> port));
>   server.run();
>
> Thanks,
>
> Chris.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: ServerSocket from within Tapestry

Christopher
In reply to this post by Christopher
Hi Eugen,

Thank you for explaining so well.  I had been considering running a
standalone WebSocket server to avoid being tied to Tomcat in particular,
but, after further thought, think I'll take your advice and keep it
simple.  :-)

Here is a recording of the client app running in demo mode:
www.harbour.nz/demo.mp4

Much thanks,

Chris.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." - Antoine de Saint-Exupéry.