TOMCAT7 Build WebSocket Service

Source: Internet
Author: User

Pom.xml ( Although the Maven TOMCAT7 plugin is configured, but the Web service is started directly from Maven, the WebSocket service cannot start.) You want to deploy the Web service to TOMCAT7 server. You can package the war directly, or you can introduce TOMCAT7 in the development tools)

<project xmlns= "http://maven.apache.org/POM/4.0.0"  xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "         xsi:schemalocation=" http// Maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">    < modelversion>4.0.0</modelversion>    <groupid>web</groupid>     <artifactid>web</artifactid>    <packaging>war</ packaging>    <version>1.0-snapshot</version>    < name>web maven webapp</name>    <url>http://maven.apache.org</ url>    <dependencies>        < Dependency>            <groupid> javax.websocket</groupid>            <artifactid>javax.websocket-api</artifactid>             <version>1.1</version>             <scope>provided</scope>         </dependency>    </dependencies>     <build>        <finalname>web</ finalname>        <plugins>             <plugin>                 <groupId>org.apache.tomcat.maven</groupId>                 <artifactId> Tomcat7-maven-plugin</artifactid>&nbSp;               <version> 2.1</version>                 <configuration>                     <port>8077</port>                     <path>/web</path >                     <uriEncoding>UTF-8</uriEncoding>                     <finalName>web</finalName>                      <server>tomcat7</server>                 </configuration>            </ Plugin>        </plugins>    </build ></project>

websocket.server.simpleserver

package websocket.server;import javax.websocket.*;import javax.websocket.server.serverendpoint; import java.io.ioexception;import java.util.logging.logger;/** * created by  administrator on 2016/4/17. */@ServerEndpoint ("/simplewebsocket") public class  Simpleserver {    private logger logger = logger.getlogger ( This.getclass (). GetName ());     @OnOpen     public void onopen (session session) {        logger.info ("websocket open ... ");  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (session);    }     /**     *  connection Close method of call      */      @OnClose     public void onclose () {         loggEr.info ("websocket close .... ");    }     @OnError     public void onerror (throwable e) {         logger.severe ("error~~~~" +e.getmessage ());     }    /**      *  methods that are called after a client message is received      *  @param  message   Messages sent by clients      *  @param  session  Optional Parameters       */     @OnMessage     public void onmessage (string  Message, session session)  throws IOException {         logger.info ("receive the message from client [" +message+ "]");         session.getbasicremote (). SendText ("The server received information from the client:" +message);     }}

simple.html

<! doctype html>


TOMCAT7 Build WebSocket Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.