简体   繁体   中英

Session Identification in Tyrus WebSocket API

I have implemented a websocket server which acts as observer for some events.

@ServerEndPoint
public class Server implements SomeObserver

I have implemented objectChanged() from SomeObserver class. The objectChanged() will execute when there is some event will be occur. It is common observer implemnetation.

The application logic is like this: Clients connect to Websocket server and server sends appropriate events for appropriate clients.

I have coded it like this:

@ServerEndPoint
public class Server implements SomeObserver
{
  Session clientSession = null;

 @OnOpen
 public void OnOpen(Session session}
 { 
  clientSession = session;
 }

 //implemented OnMessage and OnClose, OnError methods

 public void objectChanged(Event[] event)
 {
   clientSession.sendAsyncRemote().sendObject(someObjectInfo);
 }

I never used any session identification. But surprisingly, server sends appropriate messages for respective session. Server does not send one sessions event to another session without any session authentication or identification.

Does anyone know why and how it happens in Tyrus API. I want to know how Tyrus webocket support session identification.

clientSession.sendAsyncRemote().sendObject(someObjectInfo); 

In the above line, the session object will be created per connection basis. It holds the reference socket object (per connection).

Hence, when message is sent, data will be transferred to the respective client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM