WebSocket测试页面

1.HTML页面 




    
    WebScoket
    
    


Welcome

2.JS

var socket;
if(typeof(WebSocket) == "undefined") {
    console.log("您的浏览器不支持WebSocket");
}else{
    console.log("您的浏览器支持WebSocket");

    //实现化WebSocket对象,指定要连接的服务器地址与端口  建立连接
    socket = new WebSocket("ws://localhost:8002/websocket");
    //打开事件
    socket.onopen = function() {
        console.log("Socket 已打开");
    };
    //获得消息事件
    socket.onmessage = function(msg) {
        console.log(msg.data);
        playSound();
        //发现消息进入    调后台获取
        //getCallingList();
    };
    //关闭事件
    socket.onclose = function() {
        console.log("Socket已关闭");
    };
    //发生了错误事件
    socket.onerror = function() {
        alert("Socket发生了错误");
    }

    //关闭连接
    function closeWebSocket() {
        closeSound();
        socket.close();
    }

    //发送消息
    function send() {
        var message = document.getElementById('text').value;
        socket.send(message);
    }

    //播放提示声音方法
    function playSound() {
        var borswer = window.navigator.userAgent.toLowerCase();
        if ( !!window.ActiveXObject || "ActiveXObject" in window ) {
            //IE内核浏览器
            var OSPlayer = new ActiveXObject("WMPLayer.OCX");
            // OSPlayer.url = "http://www.xmf119.cn/static/admin/sounds/notify.wav";
            // OSPlayer.url = "audio/notify.wav";
            OSPlayer.url = "audio/test.mp3";
            OSPlayer.controls.play();
        } else {
            //非IE内核浏览器
            var strAudio = "

3.访问localhost:端口号点击即可测试.

你可能感兴趣的:(WebSocket测试页面)