zoukankan      html  css  js  c++  java
  • websocket

    <!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>
  • 相关阅读:
    Netty实现客户端和服务端通信简单例子
    上拉电阻的作用
    c语言常量指针赋值给变量指针导致警告
    修改ultisnips的默认键
    为debian8.2更换官方源
    linux下添加用户到sudo组
    用rfkill命令管理蓝牙和wifi
    用platformio编写arduino程序
    ubuntu下gcc-avr安装
    UNIX环境高级编程(第三版)关于apue.h的用法
  • 原文地址:https://www.cnblogs.com/flintlovesam/p/7503326.html
Copyright © 2011-2022 走看看