HTML 5 WebSocket 示例

我们通过一个简单的例子来熟悉 HTML 5 WebSocket。该例子服务端从 0 开始每秒递增一个整数并发送给客户端浏览器显示。准备:下载 Jetty 7 ,Java Web 开发环境,Chrome 4+ 。


服务端 Java 代码:
final public class CounterSocketServlet extends WebSocketServlet {

    @Override
    protected WebSocket doWebSocketConnect(final HttpServletRequest hsr,
                                           final String string) {
        return new CounterSocket();
    }

    final class CounterSocket implements WebSocket {

        private Outbound outbound;

        public void onConnect(final Outbound outbound) {
            System.out.println("onConnect");
            this.outbound = outbound;
        }

        public void onMessage(final byte frame, final String data) {
            System.out.println("onMessage");

            if (data.equals("Hello, Server!")) {
                new Thread() {

                    @Override
                    public void run() {
                        try {
                            outbound.sendMessage(frame, "Hello, browser :-)");

                            int i = 0;
                            while (true) {
                                sleep(1000);
                                outbound.sendMessage(frame, String.valueOf(i++));
                            }

                        } catch (final Exception e) {
                            System.err.println(e.getMessage());
                        }
                    }
                }.start();
            }
        }

        public void onMessage(final byte frame, final byte[] data,
                              final int offset, final int length) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public void onDisconnect() {
            System.out.println("onDisconnect");
        }
    }
}


浏览器客户端 HTML 代码:
<html>
    <head>
        <title>WebSoket Demo</title>
        <script type="text/javascript">
            if (!window.WebSocket) {
                alert("WebSocket not supported by this browser!");
            }
            
            function display() {
                var valueLabel = document.getElementById("valueLabel");
                valueLabel.innerHTML = "";
                var ws = new WebSocket(
                    "ws://localhost:8080/WebSocketDemo/counter-socket");
                ws.onmessage = function(evt) {
                    valueLabel.innerHTML = evt.data;
                };

                ws.onclose = function() {
                };

                ws.onopen = function() {
                    ws.send("Hello, Server!");
                };
            }
        </script>
    </head>
    <body οnlοad="display();">
        <div id="valueLabel"></div>
    </body>
</html>

运行:

部署
将打包好的 WebSocketDemo.war 复制到 Jetty 安装目录:


 

 
启动 Jetty
C:/jetty-distribution-7.0.1.v20091125>java -jar start.jar
测试


 

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/DL88250/archive/2010/01/01/5118301.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值