dtjo51649 2015-07-13 21:42
浏览 25
已采纳

Websockets:onmessage没有开火。!

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>hi</title>
</head>
<body>
<div id="conn"> This : </div>
<input type="text" id="text">
<input type="button" value="Button" onclick="func()" id="btn">

<script>
    var fees=document.getElementById("conn");
    var btn=document.getElementById("btn");
    var conn = new WebSocket('ws://localhost:8080');

    function func()
    {
        var text=document.getElementById("text").value;
        conn.send(text);
        conn.send('any msg');
        text.innerHTML+= text+'<br/>';
    }

    conn.onopen = function(e) {
        console.log("Connection established!");
    };

    conn.onmessage = function(e) {
        console.log(e.data);
        fees.innerHTML+= e.data+'<br/>';
    };
</script>
</body>
</html>

This my code for running websocket. i am using ratchet websockets and xampp server. Main problem is the onmessage is not firing ! (I don't know why).

Console shows that connection established!. No more errors on console. Please help ... thanks in advance.

  • 写回答

1条回答 默认 最新

  • dszsajhd237437 2015-10-21 11:05
    关注

    I have successfully implemented Socket.io websockets.

    I think it is easier to implement that Ratchet websocket

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?