websocket
Posted 与非朋仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于websocket的主要内容,如果未能解决你的问题,请参考以下文章