<!DOCTYPE html>
<html>
<head>
    <title>django-websocket</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">//<![CDATA[
    $(function () {
        $(‘#send_message‘).click(function () {
            //var socket = new WebSocket("ws://" + window.location.host + "/ws/");
            
            //var socket = new WebSocket("ws://" + "192.168.23.128:8888" + "/ws/");
            var socket = new WebSocket("wss://" + "192.168.13.76" + "/websocket/");
            socket.onopen = function () {
                console.log(‘WebSocket open‘);//成功连接上Websocket
                socket.send($(‘#message‘).val());//发送数据到服务端
            };
            socket.onmessage = function (e) {
                console.log(‘message: ‘ + e.data);//打印服务端返回的数据
                $(‘#messagecontainer‘).prepend(‘<p>‘ + e.data + ‘</p>‘);
            };
        });
    });
    //]]></script>
</head>
<body>
<br>
<input type="text" id="message" value="Hello, World!"/>
<button type="button" id="send_message">发送 message</button>
<h1>Received Messages</h1>
<div id="messagecontainer">
</div>
</body>
</html>
<!DOCTYPE html> <html> <head> <title>django-websocket</title> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript">//<![CDATA[ $(function () { $(‘#send_message‘).click(function () { //var socket = new WebSocket("ws://" + window.location.host + "/ws/"); //var socket = new WebSocket("ws://" + "192.168.23.128:8888" + "/ws/"); var socket = new WebSocket("wss://" + "192.168.13.76" + "/websocket/"); socket.onopen = function () { console.log(‘WebSocket open‘);//成功连接上Websocket socket.send($(‘#message‘).val());//发送数据到服务端 }; socket.onmessage = function (e) { console.log(‘message: ‘ + e.data);//打印服务端返回的数据 $(‘#messagecontainer‘).prepend(‘<p>‘ + e.data + ‘</p>‘); }; }); }); //]]></script> </head> <body> <br> <input type="text" id="message" value="Hello, World!"/> <button type="button" id="send_message">发送 message</button> <h1>Received Messages</h1> <div id="messagecontainer"> </div> </body> </html>
原文:http://www.cnblogs.com/flintlovesam/p/7503326.html