websocket
This commit is contained in:
parent
fb7c74480c
commit
72d6f53020
3 changed files with 101 additions and 0 deletions
39
Application/Clavardage/src/websocket/Appel.java
Normal file
39
Application/Clavardage/src/websocket/Appel.java
Normal 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();
|
||||
}
|
||||
}
|
26
Application/Clavardage/src/websocket/Client.java
Normal file
26
Application/Clavardage/src/websocket/Client.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
36
Application/Clavardage/src/websocket/Test.java
Normal file
36
Application/Clavardage/src/websocket/Test.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue