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

websocket简单样例

时间:2016-03-08 00:23:22      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

服务端

var server = require(ws).Server;
var serv = new server({port:8080});


serv.on(connection,function(socket){
    socket.send(hello client)
    socket.on(message, function (data) {
        console.log(receive:,data);
        socket.send(receive:+data)
    })
})

 页面客户端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    var socket = new WebSocket(ws://localhost:8080);
    socket.onopen = function(){
        console.log(链接成功);
        socket.send(hello server);

    }
    socket.onmessage = function(e){
        console.log(e.data)
    }

</script>
</body>
</html>

 

websocket简单样例

标签:

原文地址:http://www.cnblogs.com/liujinyu/p/5252219.html

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