菜鳥ゼロからスタートWebSocket-Server編

7351 ワード

この文章は主に参考にしたhttp://blog.csdn.net/wilsonke/article/details/22397819の手順に従います.
1.サーバ側実装(Tomcat)
Javaで実装されているwebsocketは、Server側ではTomcatに埋め込まれてサポートされていますが、WebSocketServiceletを継承するservletを開発する必要があります.普通のHttpServiceletとあまり違いはありません.
1). JAVA環境:
  • Java: jdk 1.6.0_45, Server VM 64bit
  • Maven: 3.0.5
  • Tomcat: 7.0.39.0
  • 
    ~ D:\workspace\java>java -version
    java version "1.6.0_45"
    Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
    Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
    
    ~ D:\workspace\java>mvn -version
    Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 21:51:28+0800)
    Maven home: D:\toolkit\maven3\bin\..
    Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
    Java home: D:\toolkit\java\jdk6\jre
    Default locale: zh_CN, platform encoding: GBK
    OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
    
    ~ D:\toolkit\tomcat7\bin>catalina.bat version
    Using CATALINA_BASE:   "D:\toolkit\tomcat7"
    Using CATALINA_HOME:   "D:\toolkit\tomcat7"
    Using CATALINA_TMPDIR: "D:\toolkit\tomcat7\temp"
    Using JRE_HOME:        "D:\toolkit\java\jdk6"
    Using CLASSPATH:       "D:\toolkit\tomcat7\bin\bootstrap.jar;D:\toolkit\tomcat7\bin\tomcat-juli.jar"
    Server version: Apache Tomcat/7.0.39
    Server built:   Mar 22 2013 12:37:24
    Server number:  7.0.39.0
    OS Name:        Windows 7
    OS Version:     6.1
    Architecture:   amd64
    JVM Version:    1.6.0_45-b06
    JVM Vendor:     Sun Microsystems Inc.
    

    2). mavenは簡単なwebappプロジェクトを構築します.
    
    ~ D:\workspace\java>mvn archetype:generate -DgroupId=org.conan.websocket -DartifactId=websocketServer -DarchetypeArtifactId=maven-archetype-webapp
    
    [INFO] ----------------------------------------------------------------------------
    [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
    [INFO] ----------------------------------------------------------------------------
    [INFO] Parameter: groupId, Value: org.conan.websocket
    [INFO] Parameter: packageName, Value: org.conan.websocket
    [INFO] Parameter: package, Value: org.conan.websocket
    [INFO] Parameter: artifactId, Value: websocketServer
    [INFO] Parameter: basedir, Value: D:\workspace\java
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] project created from Old (1.x) Archetype in dir: D:\workspace\java\websocketServer
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1:42.200s
    [INFO] Finished at: Tue Aug 20 13:57:05 CST 2013
    [INFO] Final Memory: 9M/179M
    [INFO] ------------------------------------------------------------------------
    

    3). プロジェクトディレクトリの構成
    
    ~ D:\workspace\java>cd websocketServer
    ~ D:\workspace\java\websocketServer>mkdir src\main\java
    ~ D:\workspace\java\websocketServer>rm src\main\webapp\index.jsp
    

    Eclipseにインポートされたプロジェクトのスクリーンショット
    菜鸟从零开始初始WebSocket -Server篇_第1张图片
    4). 編集pom.xmlプロファイル、tomcat依存性の増加
    
    ~ vi pom.xml
    
    4.0.0
    org.conan.websocket
    websocketServer
    war
    1.0-SNAPSHOT
    websocketServer Maven Webapp
    http://maven.apache.org
    
    
    org.apache.tomcat
    tomcat-catalina
    7.0.27
    provided
    
    
    org.apache.tomcat
    tomcat-coyote
    7.0.39
    provided
    
    
    
    websocketServer
    
    
    

    クラスライブラリのダウンロードとインストール
    ~ D:\workspace\java\websocketServer>mvn clean install

    5). DemoServiceletの作成、サーバ側実行クラス
    
    ~ vi src/main/java/org/conan/websocket/DemoServlet.java
    
    package org.conan.websocket;
    
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.util.ArrayList;
    
    import javax.servlet.http.HttpServletRequest;
    
    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 DemoServlet extends WebSocketServlet {
    
        private static final long serialVersionUID = -4853540828121130946L;
        private static ArrayList mmiList = new ArrayList();
    
        @Override
        protected StreamInbound createWebSocketInbound(String str, HttpServletRequest request) {
            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 {
            }
        }
    
    }
    

    6). Webを変更します.xmlファイル
    
    ~ vi src/main/webapp/WEB-INF/web.xml
    
    
    
    Archetype Created Web Application
    
    wsServlet
    org.conan.websocket.DemoServlet
    
    
    wsServlet
    /wsServlet
    
    
    


    7). コンパイル、パッケージング、tomcatへの配備
    
    ~ D:\workspace\java\websocketServer>mvn clean install

    この时はwarバッグにしてもいいですが、どうやって地元のJBOSSに伝わっても反応しませんね..原因を探して