Showing posts with label websockets. Show all posts
Showing posts with label websockets. Show all posts

Tuesday, December 19, 2017

Primefaces PushEndpoint for WebSockets

https://www.primefaces.org/docs/api/6.1/org/primefaces/push/annotation/PushEndpoint.html


"An Endpoint for Push operation executed from by RemoteEndpoint or using an EventBus. A class annotated with this annotation must at least have one method annotated with OnOpen, OnClose or OnMessage."


https://www.primefaces.org/primefaces-push-2-0/


https://github.com/Atmosphere/atmosphere



https://www.primefaces.org/showcase/push/notify.xhtml



Monday, December 11, 2017

WebSockets and JSONDecoder in Wildfly 11


import java.io.Serializable;
import java.net.URI;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import com.pierre.inst.model.Member;

@ClientEndpoint(decoders={JSONDecoder.class})
public class MemberClientEndpoint implements Serializable{

    @Inject
    private Event<Member> memberEvent;

    @OnMessage
    public void onMessage(Member member) {
        memberEvent.fire(member);
    }
}




java.lang.RuntimeException: javax.websocket.DeploymentException: UT003029: Could not find message parameter on method public void com.pierre.inst.websockets.MemberClientEndpoint.onMessage(com.pierre.inst.model.Member)

where JSONDecoder is

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

import com.google.gson.Gson;
import com.pierre.inst.model.Member;

public class JSONDecoder implements Decoder.Text<Object> {

 private Gson gson;

 @Override
 public void init(EndpointConfig config) {
  gson = new Gson();
 }

 @Override
 public void destroy() {
  // do nothing
 }

 @Override
 public Object decode(String s) throws DecodeException {
  return gson.fromJson(s, Member.class);
 }

 @Override
 public boolean willDecode(String s) {
  return true;
 }

}


see also
https://docs.oracle.com/javaee/7/tutorial/websocket007.htm
and
https://issues.jboss.org/browse/UNDERTOW-287

The issue is that JSONDecoder is too generic, parameter class is Object, it should be Member:


import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

import com.google.gson.Gson;
import com.pierre.inst.model.Member;

public class JSONDecoder implements Decoder.Text<Member> {

 private Gson gson;

 @Override
 public void init(EndpointConfig config) {
  gson = new Gson();
 }

 @Override
 public void destroy() {
  // do nothing
 }

 @Override
 public Member decode(String s) throws DecodeException {
  return gson.fromJson(s, Member.class);
 }

 @Override
 public boolean willDecode(String s) {
  return true;
 }

}







Wednesday, December 6, 2017

WebSockets client-server example

Server-side:

annotate a POJO with javax.websocket.server.ServerEndpoint

A method annotated with javax.websocket.OnOpen will handle the creation of a javax.websocket.Session

Another method handles the reception of a message, it's annotated with javax.websocket.OnMessage


Client-side:

You have to extend javax.websocket.Endpoint
and override the method onOpen(Session session, EndpointConfig config), where you add to the Session a MessageHandler.Whole to implement a onMessage() . This Endpoint just handles the Message but not the session.

To create a session, connect to the Server: ContainerProvider.getWebSocketContainer().connectToServer(this.endpoint, new URI("ws://server:port/contextroot/serviceuri"))


Client and server are here

https://github.com/vernetto/JavaMonAmour/tree/master/pvabwebsocketsserver

https://github.com/vernetto/JavaMonAmour/tree/master/pvabwebsocketsclient


Here the explanation by the Great Adam Bien





Tuesday, November 28, 2017

WebSockets on WildFly and Eclipse

I have followed this (messy) tutorial, and eventually I have made it work on Wildfly - after a long struggle

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html#overview


Here is the github repository https://github.com/vernetto/websocket


Just enter in 2 different browser this URL http://localhost:8080/WebsocketHome/


You need these classes

https://docs.oracle.com/javaee/7/api/javax/websocket/ClientEndpoint.html "POJO is a web socket client" and has a method annotated with @Message https://docs.oracle.com/javaee/7/api/javax/websocket/OnMessage.html