Last active
July 27, 2024 04:26
-
-
Save chitan/9761824 to your computer and use it in GitHub Desktop.
How to use WebSocket of Tomcat7.0.52
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Tomcat WebSocket Chat</title> | |
<script> | |
var ws = new WebSocket("ws://localhost:8080/WsChat/wschat"); | |
ws.onopen = function(){ | |
}; | |
ws.onmessage = function(message){ | |
document.getElementById("chatlog").textContent += message.data + "\n"; | |
}; | |
function postToServer(){ | |
ws.send(document.getElementById("msg").value); | |
document.getElementById("msg").value = ""; | |
} | |
function closeConnect(){ | |
ws.close(); | |
} | |
</script> | |
</head> | |
<body> | |
<textarea id="chatlog" readonly></textarea><br/> | |
<input id="msg" type="text" /> | |
<button type="submit" id="sendButton" onClick="postToServer()">Send!</button> | |
<button type="submit" id="sendButton" onClick="closeConnect()">End</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This sample is how to use websocket of Tomcat7.0.52. | |
//web.xml is not required. Because you can use Servlet3.0 Annotation. | |
package wsapp; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import javax.websocket.OnClose; | |
import javax.websocket.OnMessage; | |
import javax.websocket.OnOpen; | |
import javax.websocket.Session; | |
import javax.websocket.server.ServerEndpoint; | |
@ServerEndpoint(value = "/wschat") | |
public class WsChat{ | |
//notice:not thread-safe | |
private static ArrayList<Session> sessionList = new ArrayList<Session>(); | |
@OnOpen | |
public void onOpen(Session session){ | |
try{ | |
sessionList.add(session); | |
//asynchronous communication | |
session.getBasicRemote().sendText("Hello!"); | |
}catch(IOException e){} | |
} | |
@OnClose | |
public void onClose(Session session){ | |
sessionList.remove(session); | |
} | |
@OnMessage | |
public void onMessage(String msg){ | |
try{ | |
for(Session session : sessionList){ | |
//asynchronous communication | |
session.getBasicRemote().sendText(msg); | |
} | |
}catch(IOException e){} | |
} | |
} |
ArrayList is not thread-safe, so I think concurrency problems can occur with your code
Hi, I followed the same thing but getting 404 error
Stuck with the 404 error for a while, here is how I get it to work:
- maven dependence using provided scope
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
very useful,very simple.
Hello, I have derived this into a more complex example - containing list of active users; also maven-compileable (full pom.xml).
And also made the global fields thread-safe.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!