Skip to content

Commit 7073045

Browse files
author
oguzhan.onder
committed
Websocket örnek çalışma
0 parents  commit 7073045

9 files changed

+440
-0
lines changed

pom.xml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>WebSocketExample</groupId>
8+
<artifactId>WebSocketExample</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<jetty-version>9.2.7.v20150116</jetty-version>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<configuration>
22+
<source>8</source>
23+
<target>8</target>
24+
</configuration>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
29+
<dependencies>
30+
31+
<dependency>
32+
<groupId>javax.websocket</groupId>
33+
<artifactId>javax.websocket-api</artifactId>
34+
<version>1.1</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.eclipse.jetty.websocket</groupId>
39+
<artifactId>javax-websocket-server-impl</artifactId>
40+
<version>${jetty-version}</version>
41+
</dependency>
42+
<!-- To run javax.websocket client -->
43+
<dependency>
44+
<groupId>org.eclipse.jetty.websocket</groupId>
45+
<artifactId>javax-websocket-client-impl</artifactId>
46+
<version>${jetty-version}</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>com.google.code.gson</groupId>
51+
<artifactId>gson</artifactId>
52+
<version>2.8.0</version>
53+
</dependency>
54+
55+
</dependencies>
56+
</project>

src/main/index.html

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<head>
3+
<title>Chat</title>
4+
</head>
5+
<body>
6+
7+
<table>
8+
<tr>
9+
<td colspan="2">
10+
<input type="text" id="username" placeholder="Username"/>
11+
<button type="button" onclick="connect();" >Connect</button>
12+
</td>
13+
</tr>
14+
<tr>
15+
<td>
16+
<textarea readonly="true" rows="10" cols="80" id="log"></textarea>
17+
</td>
18+
</tr>
19+
<tr>
20+
<td>
21+
<input type="text" size="51" id="msg" placeholder="Message"/>
22+
<button type="button" onclick="send();" >Send</button>
23+
</td>
24+
</tr>
25+
</table>
26+
</body>
27+
28+
<script src="websocket.js"></script>
29+
30+
</html>

src/main/java/ChatEndpoint.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
import javax.websocket.*;
3+
import javax.websocket.server.PathParam;
4+
import javax.websocket.server.ServerEndpoint;
5+
import java.io.IOException;
6+
import java.util.HashMap;
7+
import java.util.Set;
8+
import java.util.concurrent.CopyOnWriteArraySet;
9+
10+
@ServerEndpoint(
11+
value = "/chat/{username}",
12+
decoders = MessageDecoder.class,
13+
encoders = MessageEncoder.class
14+
)
15+
public class ChatEndpoint {
16+
17+
private Session session;
18+
private static Set<ChatEndpoint> chatEndpoints
19+
= new CopyOnWriteArraySet<>();
20+
private static HashMap<String, String> users = new HashMap<>();
21+
22+
@OnOpen
23+
public void onOpen(
24+
Session session,
25+
@PathParam("username") String username) throws IOException, EncodeException {
26+
27+
this.session = session;
28+
chatEndpoints.add(this);
29+
users.put(session.getId(), username);
30+
31+
Message message = new Message();
32+
message.setFrom(username);
33+
message.setContent("Connected!");
34+
broadcast(message);
35+
}
36+
37+
@OnMessage
38+
public void onMessage(Session session, Message message)
39+
throws IOException, EncodeException {
40+
41+
message.setFrom(users.get(session.getId()));
42+
broadcast(message);
43+
}
44+
45+
@OnClose
46+
public void onClose(Session session) throws IOException, EncodeException {
47+
48+
chatEndpoints.remove(this);
49+
Message message = new Message();
50+
message.setFrom(users.get(session.getId()));
51+
message.setContent("Disconnected!");
52+
broadcast(message);
53+
}
54+
55+
@OnError
56+
public void onError(Session session, Throwable throwable) {
57+
// Do error handling here
58+
}
59+
60+
private static void broadcast(Message message)
61+
throws IOException, EncodeException {
62+
63+
chatEndpoints.forEach(endpoint -> {
64+
synchronized (endpoint) {
65+
try {
66+
endpoint.session.getBasicRemote().
67+
sendObject(message);
68+
} catch (IOException | EncodeException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
});
73+
}
74+
}

src/main/java/Demo.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import org.eclipse.jetty.server.Server;
2+
import org.eclipse.jetty.server.ServerConnector;
3+
import org.eclipse.jetty.servlet.ServletContextHandler;
4+
import org.eclipse.jetty.servlet.ServletHolder;
5+
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
6+
7+
import javax.websocket.server.ServerContainer;
8+
9+
public class Demo {
10+
11+
public static void main(String[] args) {
12+
Server server = new Server();
13+
ServerConnector connector = new ServerConnector(server);
14+
connector.setPort(8080);
15+
server.addConnector(connector);
16+
17+
// Setup the basic application "context" for this application at "/"
18+
// This is also known as the handler tree (in jetty speak)
19+
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
20+
context.setContextPath("/");
21+
server.setHandler(context);
22+
23+
try
24+
{
25+
// Initialize javax.websocket layer
26+
ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(context);
27+
28+
// Add WebSocket endpoint to javax.websocket layer
29+
wscontainer.addEndpoint(ChatEndpoint.class);
30+
31+
server.start();
32+
server.dump(System.err);
33+
server.join();
34+
}
35+
catch (Throwable t)
36+
{
37+
t.printStackTrace(System.err);
38+
}
39+
}
40+
}

src/main/java/Message.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Message {
2+
3+
private String from;
4+
private String to;
5+
private String content;
6+
7+
public String getFrom() {
8+
return from;
9+
}
10+
11+
public void setFrom(String from) {
12+
this.from = from;
13+
}
14+
15+
public String getTo() {
16+
return to;
17+
}
18+
19+
public void setTo(String to) {
20+
this.to = to;
21+
}
22+
23+
public String getContent() {
24+
return content;
25+
}
26+
27+
public void setContent(String content) {
28+
this.content = content;
29+
}
30+
}

src/main/java/MessageDecoder.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import com.google.gson.Gson;
2+
3+
import javax.websocket.DecodeException;
4+
import javax.websocket.Decoder;
5+
import javax.websocket.EndpointConfig;
6+
7+
public class MessageDecoder implements Decoder.Text<Message> {
8+
9+
private static Gson gson = new Gson();
10+
11+
12+
public Message decode(String s) throws DecodeException {
13+
return gson.fromJson(s, Message.class);
14+
}
15+
16+
public boolean willDecode(String s) {
17+
return (s != null);
18+
}
19+
20+
public void init(EndpointConfig endpointConfig) {
21+
22+
}
23+
24+
public void destroy() {
25+
26+
}
27+
}

src/main/java/MessageEncoder.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import com.google.gson.Gson;
2+
3+
import javax.websocket.EncodeException;
4+
import javax.websocket.Encoder;
5+
import javax.websocket.EndpointConfig;
6+
7+
public class MessageEncoder implements Encoder.Text<Message> {
8+
9+
private static Gson gson = new Gson();
10+
11+
public String encode(Message message) throws EncodeException {
12+
return gson.toJson(message); }
13+
14+
public void init(EndpointConfig endpointConfig) {
15+
16+
}
17+
18+
public void destroy() {
19+
20+
}
21+
}

0 commit comments

Comments
 (0)