-
-
Save liulixiang1988/1d7d06d5c321c7e3d59337a755f5a0f4 to your computer and use it in GitHub Desktop.
How to use WebSocket of Tomcat
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/TomcatWebSocket/wschat/WsChatServlet"); | |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> | |
<servlet> | |
<servlet-name>WsChatServlet</servlet-name> | |
<servlet-class>wsapp.WsChatServlet</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>WsChatServlet</servlet-name> | |
<url-pattern>/wschat/WsChatServlet</url-pattern> | |
</servlet-mapping> | |
</web-app> |
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 Tomcat. | |
package wsapp; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.CharBuffer; | |
import java.util.ArrayList; | |
import org.apache.catalina.websocket.MessageInbound; | |
import org.apache.catalina.websocket.StreamInbound; | |
import org.apache.catalina.websocket.WebSocketServlet; | |
import org.apache.catalina.websocket.WsOutbound; | |
public class WsChatServlet extends WebSocketServlet{ | |
private static final long serialVersionUID = 1L; | |
private static ArrayList<MyMessageInbound> mmiList = new ArrayList<MyMessageInbound>(); | |
public StreamInbound createWebSocketInbound(String protocol){ | |
return new MyMessageInbound(); | |
} | |
private class MyMessageInbound extends MessageInbound{ | |
WsOutbound myoutbound; | |
@Override | |
public void onOpen(WsOutbound outbound){ | |
try { | |
System.out.println("Open Client."); | |
this.myoutbound = outbound; | |
mmiList.add(this); | |
outbound.writeTextMessage(CharBuffer.wrap("Hello!")); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onClose(int status){ | |
System.out.println("Close Client."); | |
mmiList.remove(this); | |
} | |
@Override | |
public void onTextMessage(CharBuffer cb) throws IOException{ | |
System.out.println("Accept Message : "+ cb); | |
for(MyMessageInbound mmib: mmiList){ | |
CharBuffer buffer = CharBuffer.wrap(cb); | |
mmib.myoutbound.writeTextMessage(buffer); | |
mmib.myoutbound.flush(); | |
} | |
} | |
@Override | |
public void onBinaryMessage(ByteBuffer bb) throws IOException{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment