码迷,mamicode.com
首页 > Web开发 > 详细

websocket

时间:2017-09-11 10:08:52      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:html   col   location   htm   连接   web   query   rip   code   

<!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>

 

websocket

标签:html   col   location   htm   连接   web   query   rip   code   

原文地址:http://www.cnblogs.com/flintlovesam/p/7503326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!