[jetty-users] WebSockets: Specific Client Message

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

[jetty-users] WebSockets: Specific Client Message

Hendrik Schenk
hi,

I have 2 questions:

1. Is there an opportunity to send a message from server to a specific client which is connected to server?

2. At the moment, i use a servlet fpr the communication between the clients and the server. Is there another way to communicate with the websockets server? I mean, i need a interface where the clients can connect to and send and retrieve messages.


Thank your for help in advance!

Best reagards
hschenk


_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Simone Bordet-2
Hi,

On Tue, Jan 10, 2012 at 15:30, Hendrik Schenk <[hidden email]> wrote:
> hi,
>
> I have 2 questions:
>
> 1. Is there an opportunity to send a message from server to a specific
> client which is connected to server?

Yes.

> 2. At the moment, i use a servlet fpr the communication between the clients
> and the server. Is there another way to communicate with the websockets
> server? I mean, i need a interface where the clients can connect to and send
> and retrieve messages.

See http://wiki.eclipse.org/Jetty/Feature/WebSockets as a starting point.

What you need is to extend WebSocketServlet and implement doWebSocketConnect().

That method is invoked for each remote client connection.
You should return an implementation of a WebSocket sub-interface, most
often WebSocket.OnTextMessage.
This object is linked with the remote client and allows you to handle
passive events (i.e. events generated by the remote client).

To generate events you can use the WebSocket.Connection API. You
obtain an instance of this interface in WebSocket.onOpen() and you can
use it to send messages and close the connection.

See https://github.com/eclipse/jetty.project/blob/master/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java.

Or, you can look at CometD for a simpler way to use WebSocket
features: http://cometd.org.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Hendrik Schenk
Ok, so far. That is what i use already ;)

But  i get a problem with this example, if i use multiple clients
respectively parallel requests/connections .

If two or more clients connect to the server and the server answers,
each client get all answers.

For example:

Client 1 sends "hello"
Client 2 sends "world"  (at the same time)

Client 1 gets hello world or world hello
Client 2 gets the same.


I solved the problem by avoiding  "private final Set<ChatWebSocket>
_members = new CopyOnWriteArraySet<ChatWebSocket>();" in my
Servlet-Class and in my Server-Class. Where is that good for?!?! I do
not understand it. It works without it, too, and especially correctly.

best regrads
hschenk



Simone Bordet schrieb:

> Hi,
>
> On Tue, Jan 10, 2012 at 15:30, Hendrik Schenk <[hidden email]> wrote:
>  
>> hi,
>>
>> I have 2 questions:
>>
>> 1. Is there an opportunity to send a message from server to a specific
>> client which is connected to server?
>>    
>
> Yes.
>
>  
>> 2. At the moment, i use a servlet fpr the communication between the clients
>> and the server. Is there another way to communicate with the websockets
>> server? I mean, i need a interface where the clients can connect to and send
>> and retrieve messages.
>>    
>
> See http://wiki.eclipse.org/Jetty/Feature/WebSockets as a starting point.
>
> What you need is to extend WebSocketServlet and implement doWebSocketConnect().
>
> That method is invoked for each remote client connection.
> You should return an implementation of a WebSocket sub-interface, most
> often WebSocket.OnTextMessage.
> This object is linked with the remote client and allows you to handle
> passive events (i.e. events generated by the remote client).
>
> To generate events you can use the WebSocket.Connection API. You
> obtain an instance of this interface in WebSocket.onOpen() and you can
> use it to send messages and close the connection.
>
> See https://github.com/eclipse/jetty.project/blob/master/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java.
>
> Or, you can look at CometD for a simpler way to use WebSocket
> features: http://cometd.org.
>
> Simon
>  
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Simone Bordet-2
Hi,

On Tue, Jan 10, 2012 at 19:37, Hendrik Schenk <[hidden email]> wrote:

> Ok, so far. That is what i use already ;)
>
> But  i get a problem with this example, if i use multiple clients
> respectively parallel requests/connections .
>
> If two or more clients connect to the server and the server answers, each
> client get all answers.
>
> For example:
>
> Client 1 sends "hello"
> Client 2 sends "world"  (at the same time)
>
> Client 1 gets hello world or world hello
> Client 2 gets the same.
>
>
> I solved the problem by avoiding  "private final Set<ChatWebSocket> _members
> = new CopyOnWriteArraySet<ChatWebSocket>();" in my Servlet-Class and in my
> Server-Class. Where is that good for?!?! I do not understand it.

It's a single room chat: if a client sends a message, all other
clients must receive it.
That's why the example is iterating over the _members in onMessage().

If you want to reply (and not broadcast), just use the Connection
object to reply to the client that sent the message.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Hendrik Schenk
Oh...how stupid of me!

So it was correct what i did. thank you for your help.

And a servlet is the correct way to use websockets in jetty? or is there
another way?

best regards
hschenk

Simone Bordet schrieb:

> Hi,
>
> On Tue, Jan 10, 2012 at 19:37, Hendrik Schenk <[hidden email]> wrote:
>  
>> Ok, so far. That is what i use already ;)
>>
>> But  i get a problem with this example, if i use multiple clients
>> respectively parallel requests/connections .
>>
>> If two or more clients connect to the server and the server answers, each
>> client get all answers.
>>
>> For example:
>>
>> Client 1 sends "hello"
>> Client 2 sends "world"  (at the same time)
>>
>> Client 1 gets hello world or world hello
>> Client 2 gets the same.
>>
>>
>> I solved the problem by avoiding  "private final Set<ChatWebSocket> _members
>> = new CopyOnWriteArraySet<ChatWebSocket>();" in my Servlet-Class and in my
>> Server-Class. Where is that good for?!?! I do not understand it.
>>    
>
> It's a single room chat: if a client sends a message, all other
> clients must receive it.
> That's why the example is iterating over the _members in onMessage().
>
> If you want to reply (and not broadcast), just use the Connection
> object to reply to the client that sent the message.
>
> Simon
>  
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Simone Bordet-2
Hi,

On Tue, Jan 10, 2012 at 20:25, Hendrik Schenk <[hidden email]> wrote:
> Oh...how stupid of me!
>
> So it was correct what i did. thank you for your help.
>
> And a servlet is the correct way to use websockets in jetty? or is there
> another way?

WebSocketServlet is the best way.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Seonhong Ahn
Hi..

According to jetty Web application architecture,

ServletHandler is located in the lastest.. 

So, I tried to adapt DefaultHandler to WebSocketHandler... but it didn't work..

I don't know why...

Is really there no way except Servlet for WebSocket..?

This is my source..

-------------------------------------------------------------

public class PocWebSocketServletContextListener implements ServletContextListener{

    private Server server;
   
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        if (server != null) {
            try {
                // stop the Jetty server.
                server.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
       
        System.out.println("PocWebSocketServletContextListener has received a servlet context event.");
        try{
            this.server = new Server(8081);

            PocWebSocketHandler handler = new PocWebSocketHandler();
            handler.setHandler(new org.eclipse.jetty.server.handler.DefaultHandler());
            server.setHandler(handler);

            server.start();
        }catch(Throwable e){
            e.printStackTrace();
        }
    }
}
--------------
public class PocWebSocketHandler extends WebSocketHandler{

    public PocWebSocketHandler(){
        System.out.println("PocWebSocketHandler is created.");
    }
   
    @Override
    public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
       
        return new PocWebSocket();
    }
}
---------------------------------------

Thanks in advance.

Sunny


On Wed, Jan 11, 2012 at 4:35 AM, Simone Bordet <[hidden email]> wrote:
Hi,

On Tue, Jan 10, 2012 at 20:25, Hendrik Schenk <[hidden email]> wrote:
> Oh...how stupid of me!
>
> So it was correct what i did. thank you for your help.
>
> And a servlet is the correct way to use websockets in jetty? or is there
> another way?

WebSocketServlet is the best way.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users



--

----------------------------------------------------------------------------------
Seonhong Ahn
Director of Research and Development

Collabra Co.
960-6 Deachi-dong, Gangnam-gu, Seoul, Korea 135-280 
Tel. 82-70-8670-6929         Fax. 82-2-6280-4807
Mobile 82-10-8292-3923
----------------------------------------------------------------------------------


_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Simone Bordet-2
Hi,

On Thu, Jan 12, 2012 at 02:54, Seonhong Ahn <[hidden email]> wrote:

> Hi..
>
> According to jetty Web application architecture,
>
> ServletHandler is located in the lastest..
>
> So, I tried to adapt DefaultHandler to WebSocketHandler... but it didn't
> work..
>
> I don't know why...

You cannot use a ServletContextListener, which is an entity that is
attached to a Server, to start and stop the server itself.

> Is really there no way except Servlet for WebSocket..?

That's the simplest way. You build a standard web application, you
create your WebSocketServlet subclass, map it in web.xml, and you're
done.

I'd recommend to start with that.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: [jetty-users] WebSockets: Specific Client Message

Seonhong Ahn

Simon,

Thanks a lot.

Sunny


On Thu, Jan 12, 2012 at 7:14 PM, Simone Bordet <[hidden email]> wrote:
Hi,

On Thu, Jan 12, 2012 at 02:54, Seonhong Ahn <[hidden email]> wrote:
> Hi..
>
> According to jetty Web application architecture,
>
> ServletHandler is located in the lastest..
>
> So, I tried to adapt DefaultHandler to WebSocketHandler... but it didn't
> work..
>
> I don't know why...

You cannot use a ServletContextListener, which is an entity that is
attached to a Server, to start and stop the server itself.

> Is really there no way except Servlet for WebSocket..?

That's the simplest way. You build a standard web application, you
create your WebSocketServlet subclass, map it in web.xml, and you're
done.

I'd recommend to start with that.

Simon
--
http://cometd.org
http://intalio.com
http://bordet.blogspot.com
----
Finally, no matter how good the architecture and design are,
to deliver bug-free software with optimal performance and reliability,
the implementation technique must be flawless.   Victoria Livschitz
_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users



--

----------------------------------------------------------------------------------
Seonhong Ahn 
Director of Research and Development

Collabra Co.
960-6 Deachi-dong, Gangnam-gu, Seoul, Korea 135-280 
Tel. 82-70-8670-6929         Fax. 82-2-6280-4807
Mobile 82-10-8292-3923
----------------------------------------------------------------------------------


_______________________________________________
jetty-users mailing list
[hidden email]
https://dev.eclipse.org/mailman/listinfo/jetty-users