Imported code from secret source for websockets (@tommi27 you don't know anything about it, right?)
This commit is contained in:
parent
1e2bb75181
commit
589ef8c3cc
4 changed files with 123 additions and 0 deletions
|
@ -0,0 +1,26 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
||||
|
||||
import javax.websocket.server.ServerEndpointConfig;
|
||||
|
||||
public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
|
||||
|
||||
public static SensorSocketEndpoint getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static SensorSocketEndpoint instance = new SensorSocketEndpoint();
|
||||
|
||||
@Override
|
||||
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
final T instance = (T) SensorSocketConfig.instance;
|
||||
return instance;
|
||||
} catch (ClassCastException e) {
|
||||
final var e2 =
|
||||
new InstantiationException("Cannot cast SensorSocketEndpoint to desired type");
|
||||
e2.initCause(e);
|
||||
throw e2;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import javax.websocket.*;
|
||||
|
||||
public class SensorSocketDecoder implements Decoder.Text<JsonObject> {
|
||||
private Gson decoder;
|
||||
|
||||
@Override
|
||||
public void init(EndpointConfig endpointConfig) {
|
||||
decoder = new Gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {}
|
||||
|
||||
@Override
|
||||
public JsonObject decode(String s) throws DecodeException {
|
||||
try {
|
||||
return decoder.fromJson(s, JsonObject.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new DecodeException(s, "Cannot decode sensor message", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean willDecode(String s) {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import javax.websocket.EncodeException;
|
||||
import javax.websocket.Encoder;
|
||||
import javax.websocket.EndpointConfig;
|
||||
|
||||
public class SensorSocketEncoder implements Encoder.Text<JsonObject> {
|
||||
private Gson encoder;
|
||||
|
||||
@Override
|
||||
public String encode(JsonObject object) throws EncodeException {
|
||||
return encoder.toJson(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(EndpointConfig endpointConfig) {
|
||||
encoder = new Gson();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
|
||||
@ServerEndpoint(
|
||||
value = "/service",
|
||||
configurator = SensorSocketConfig.class,
|
||||
encoders = SensorSocketEncoder.class,
|
||||
decoders = SensorSocketDecoder.class)
|
||||
public class SensorSocketEndpoint {
|
||||
|
||||
private Set<Session> clients = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
public Set<Session> getClients() {
|
||||
return clients;
|
||||
}
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session userSession) {
|
||||
clients.add(userSession);
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session userSession) {
|
||||
clients.remove(userSession);
|
||||
}
|
||||
|
||||
public int broadcast(JsonObject message) throws IOException, EncodeException {
|
||||
for (Session session : clients) {
|
||||
System.out.println(message);
|
||||
session.getBasicRemote().sendObject(message);
|
||||
}
|
||||
return clients.size();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue