websocket

This commit is contained in:
LMAGallois 2021-02-09 20:54:17 +01:00
parent fb7c74480c
commit 72d6f53020
3 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,39 @@
package websocket;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
public class Appel {
private WebSocketContainer container;
private Client client;
public void test() {
try {
Session session=this.container.connectToServer(this.client, new URI ("ws://localhost:8080/Clavardage/hello"));
} catch (DeploymentException | IOException | URISyntaxException e) {
e.printStackTrace();
System.out.println("pb avec le connect");
}
try {
this.client.sendMessage("hello from client");
} catch (IOException e) {
System.out.println("pb avec le sendMessage");
e.printStackTrace();
}
}
public void onInit(){
this.container= ContainerProvider.getWebSocketContainer();
this.client=new Client();
}
}

View file

@ -0,0 +1,26 @@
package websocket;
import java.io.IOException;
import javax.websocket.*;
public class Client extends Endpoint{
private Session session;
@Override
public void onOpen (Session session, EndpointConfig config) {
this.session=session;
this.session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage (String msg) {
System.out.println("msg recu:"+msg);
};}
);
}
public void sendMessage(String msg) throws IOException {
this.session.getBasicRemote().sendText(msg);
}
}

View file

@ -0,0 +1,36 @@
package websocket;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import org.junit.Before;
class Test {
@org.junit.jupiter.api.Test
void test() throws IOException, DeploymentException, URISyntaxException {
Session session=this.container.connectToServer(this.client, new URI ("ws://localhost:8080/Clavardage/hello"));
this.client.sendMessage("hello from client");
}
private WebSocketContainer container;
private Client client;
@Before
public void onInit(){
this.container= ContainerProvider.getWebSocketContainer();
this.client=new Client();
}
}