I am writing a simple websocket application. In this I separate out the message sending functionality. More clearly onOpen, onClose, onMessage, onError
etc methods are placed in the class which annotated with @ServerEndpoint
ie
@ServerEndpoint(value = "/test")
public class TestEndpoint {
@OnOpen
public void onOpen(Session session) throws IOException {
}
@OnMessage
public String echo(String message) {
}
@OnError
public void onError(Throwable t) {
}
@OnClose
public void onClose(Session session) {
}
}
And I want to give responsibility of sending message object to the class MessageSender
, ie
public class MessageSender {
/**
*
* This method will take care of sending messages object
*/
public void sendMessage(){
}
}
so MessageSender class should make use of websocket Encoder
and Decoder
class but as per my knowledge we can use Encoders
and Decoders
only in @ServerEndpoint
annotation
like,
@ServerEndpoint(value = "/test", encoder = Encoder.class, decoders = Decoder.class)
is there any way to use encoder and decoder for simple class?
Encoder
和Decoder
将应用于创建的Session
对象,因此,如果将其传递给MessageSender
类,然后执行诸如session.getBasicRemote().sendObject(…)
,它将使用在上定义的同一组编码器@ServerEndpoint
批注。
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.