-
-
Save chitan/3063774 to your computer and use it in GitHub Desktop.
<!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> |
<?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 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{ | |
} | |
} | |
} |
This code only works with Tomcat 7. Tomcat 8 the websocket classes appear to have been refactored.
@IslamBesto I'm using the tomcat version 7.0.34,And your mistakes as
I'm using Tomcat 7.0.52 version ... I cannot get the outbound part sending "Hello client" after connected, only when I turn off my tomcat, my client receives "Hello client" ... and I haven't make any changes ... why???
Btw, I changed the protocol to NIO on tomcat server.xml.
If you can use Tomcat 7.0.52, look at here.
https://gist.github.com/chitan/9761824
If anybody need pom.xml example for this project-here it is. I've just done it and test
<groupId>wschat</groupId>
<artifactId>wschat</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>wschat</name>
<url>http://example.com</url>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.39</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>7.0.27</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
@AiRmode thanks!
WebSocket is working fine on tomcat 7 but i could fidn the same jars in tomcat 8 ,please help me out
I am getting exception below exception,while staring the server.(D:\novworkspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\lexus-drivers-webapp\WEB-INF\lib\tomcat-servlet-api-7.0.27.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
can i get all the material of wesocket?
Thank a lot!
Where did you get the package "org.apache.catalina.websocket" from?
Somehow my catalina.jar (from Tomcat 9) don't has it.
And I can't find it somewhere.
Please, give me link of needed lib or official resourse url.
@pavelantushevich
WebSocketServlet Deprecated.
Replaced by the JSR356 WebSocket 1.1 implementation and will be removed in Tomcat 8.0.x.
WebSocketServlet @doc
Thank a lot! good
Thanks!
This is a great code thank you. Going to run it on my site.
Doesn't work with Tomcat 9
Can want java code for client side not the javascript code can anyone provide me plz.TQ
how to solve this error
error: package org.apache.catalina.websocket does not exist
import org.apache.catalina.websocket.MessageInbound;
Note : already import org.apache.catalina.websocket.* jar file
I am trying Dr. Danny Coward's code on pp6 of his Websocket book but has been returning an 'undefined' error for years. Could someone look at Dr. Coward's code and tell where I am getting wrong.
I'm using the tomcat version 7.0.39 and it does'nt work for me, it generate this exception :
GRAVE: "Servlet.service()" pour la servlet WsChatServlet a généré une exception
java.lang.ClassCastException: org.apache.coyote.Request cannot be cast to org.apache.coyote.http11.upgrade.UpgradeInbound