35 lines
737 B
Java
35 lines
737 B
Java
|
package websocket;
|
||
|
|
||
|
import javax.websocket.server.*;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
|
||
|
import javax.websocket.*;
|
||
|
|
||
|
@ServerEndpoint("/hello/")
|
||
|
public class Server {
|
||
|
private Session session;
|
||
|
|
||
|
@OnOpen
|
||
|
public void connect(Session session) {
|
||
|
this.session= session;
|
||
|
System.out.println("session :"+ session);
|
||
|
}
|
||
|
|
||
|
@OnClose
|
||
|
public void close() {
|
||
|
this.session=null;
|
||
|
System.out.println("session closed");
|
||
|
}
|
||
|
|
||
|
@OnMessage
|
||
|
public void onMessage(String msg) throws IOException {
|
||
|
System.out.println("msg recu:"+msg);
|
||
|
this.session.getAsyncRemote().sendText("server:"+msg);
|
||
|
//if (this.session != null && this.session.isOpen()) {
|
||
|
// this.session.getBasicRemote().sendText("server:"+msg);
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
}
|