LMAGallois 9 months ago
parent
commit
00b3ad3038
1 changed files with 34 additions and 0 deletions
  1. 34
    0
      Server.java

+ 34
- 0
Server.java View File

@@ -0,0 +1,34 @@
1
+package websocket;
2
+
3
+import javax.websocket.server.*;
4
+
5
+import java.io.IOException;
6
+
7
+import javax.websocket.*;
8
+
9
+@ServerEndpoint("/hello/")
10
+public class Server {
11
+	private Session session;
12
+	
13
+	@OnOpen
14
+	public void connect(Session session) {
15
+		this.session= session;
16
+		System.out.println("session :"+ session);
17
+	}
18
+	
19
+	@OnClose 
20
+	public void close() {
21
+		this.session=null;
22
+		System.out.println("session closed");
23
+	}
24
+	
25
+	@OnMessage
26
+	public void onMessage(String msg) throws IOException {
27
+		System.out.println("msg recu:"+msg);
28
+		this.session.getAsyncRemote().sendText("server:"+msg);
29
+		//if (this.session != null && this.session.isOpen()) {
30
+		//	this.session.getBasicRemote().sendText("server:"+msg);
31
+		//}
32
+	}
33
+
34
+}

Loading…
Cancel
Save