Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6651483
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: JavaScript

2015-12-02 17:57:41



You can use HTML5 Web Sockets:

点击(此处)折叠或打开

  1. var connection = new WebSocket('ws://IPAddress:Port');
  2. connection.onopen = function () {
  3.   connection.send('Ping'); // Send the message 'Ping' to the server
  4. };


++++++++++++++++++++++++++++++++++

See . Haven't used it myself. Been more than 3 years since last update (as of 26/6/2014).

From the documentation:


点击(此处)折叠或打开

  1. <script type='text/javascript'>
  2.     // Host we are connecting to
  3.     var host = 'localhost';
  4.     // Port we are connecting on
  5.     var port = 3000;

  6.     var socket = new jSocket();

  7.     // When the socket is added the to document
  8.     socket.onReady = function(){
  9.             socket.connect(host, port);
  10.     }

  11.     // Connection attempt finished
  12.     socket.onConnect = function(success, msg){
  13.             if(success){
  14.                     // Send something to the socket
  15.                     socket.write('Hello world');
  16.             }else{
  17.                     alert('Connection to the server could not be estabilished: ' + msg);
  18.             }
  19.     }
  20.     socket.onData = function(data){
  21.             alert('Received from socket: '+data);
  22.     }

  23.     // Setup our socket in the div with the id="socket"
  24.     socket.setup('mySocket');
  25. </script>







阅读(951) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~