简体   繁体   English

Tyrus WebSocket API中的会话标识

[英]Session Identification in Tyrus WebSocket API

I have implemented a websocket server which acts as observer for some events. 我已经实现了一个websocket服务器,它充当某些事件的观察者。

@ServerEndPoint
public class Server implements SomeObserver

I have implemented objectChanged() from SomeObserver class. 我已经从SomeObserver类实现objectChanged()。 The objectChanged() will execute when there is some event will be occur. 当发生某些事件时,将执行objectChanged()。 It is common observer implemnetation. 这是常见的观察者象征。

The application logic is like this: Clients connect to Websocket server and server sends appropriate events for appropriate clients. 应用程序逻辑如下:客户端连接到Websocket服务器,服务器为适当的客户端发送适当的事件。

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. 有谁知道Tyrus API中的原因以及如何发生。 I want to know how Tyrus webocket support session identification. 我想知道Tyrus webocket如何支持会话识别。

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. 因此,当发送消息时,数据将被传输到相应的客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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