日期:2014-05-17  浏览次数:20795 次

tomcat7.0.27支持websocket了,求哪位大侠做个用tomcat做服务器的简单的在线即时聊天demo
我看它自带的demo看不懂..API又全是英文~T_T

求救啊~~~~~~~

------解决方案--------------------
楼上的帅哥。tomcat自带例子其实很简单的,就是部署相对能有点麻烦。
Java code

package websocket;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;

import util.HTMLFilter;

public class ChatWebSocketServlet extends WebSocketServlet {
    private static final long serialVersionUID = 1L;

    private static final String GUEST_PREFIX = "Guest";

    /**
     * int操作的一个类,用来具有原子性的增加减少。
     */
    private final AtomicInteger connectionIds = new AtomicInteger(0);
    /**
     * 适合小数量。不可变。线程安全,迭代器遍历速度快的集合 用来存放信息绑定辅助类
     */
    private final Set<ChatMessageInbound> connections = new CopyOnWriteArraySet<ChatMessageInbound>();

    @Override
    protected StreamInbound createWebSocketInbound(String subProtocol) {
        return new ChatMessageInbound(connectionIds.incrementAndGet());
    }

    /**
     * 信息绑定辅助类 文件标题
     * 
     * 作者: WangZhenChong
     */
    private final class ChatMessageInbound extends MessageInbound {
        /**
         * 用户名称
         */
        private final String nickname;

        /**
         * 构造方法
         * 
         * @param id
         */
        private ChatMessageInbound(int id) {
            this.nickname = GUEST_PREFIX + id;
        }

        /**
         * 建立连接的时候
         */
        @Override
        protected void onOpen(WsOutbound outbound) {
            connections.add(this);
            String message = String.format("* %s %s", nickname, "has joined.");
            broadcast(message);
        }

        /**
         * 管理连接的时候
         */
        @Override
        protected void onClose(int status) {
            connections.remove(this);
            String message = String.format("* %s %s", nickname, "has disconnected.");
            broadcast(message);
        }

        /**
         * 获得二进制文件信息的时候
         */
        @Override
        protected void onBinaryMessage(ByteBuffer message) throws IOException {
            throw new UnsupportedOperationException("Binary message not supported.");
        }

        /**
         * 获得文本信息的时候
         */
        @Override
        protected void onTextMessage(CharBuffer message) throws IOException {
            String filteredMessage = String.format("%s: %s", nickname, HTMLFilter.filter(message.toString()));
            broadcast(filteredMessage);
        }

        /**
         * 向每个在线用户发送信息的方法
         * 
         * @param message
         */
        private void broadcast(String message) {
            for (ChatMessageInbound connection : connections) {
                try {
                    CharBuffer buffer = CharBuffer.wrap(message);
                    connection.getWsOutbound().writeTextMessage(buffer);
                } catch (IOException ignore) {
                }
            }
        }
    }
}