Socket.io客户端socket.io@0.9.16

时间:2014-07-01 20:01:28

标签: node.js socket.io

我的问题是我可以通过html / js连接到我的客户端,但我不能使用节点做同样的事情。我使用的是socket.io@0.9.16。

var io       = require('socket.io');

var socket = io.connect('https://website.com:3000');

socket.on('connect', function (data) {
    socket.emit('room', 'connecting');
})

socket.on('message' , function (data){
    console.log("message has been sent");
});

VS

<html>

<script src="https://website.com:3000/socket.io/socket.io.js"></script>


<script type="text/javascript">
    var socket = io.connect('https://website.com:3000');

    socket.on('connect', function (data) {
        socket.emit('room', 'connecting');
    })

    socket.on('message' , function (data){
        alert(data.message);
    });
</script>


</html>    

2 个答案:

答案 0 :(得分:2)

npm install socket.io-client@0.9.16

 var io       = require('socket.io-client');

var socket = io.connect('https://website:3000')    ;

  socket.on('connect', function (data) {
  socket.emit('room', 'foo');
  console.log('connecting... working...')
});

socket.on('someRoom' , function (data){
  console.log('update ...');
  console.log(data);
});

答案 1 :(得分:0)

您不希望socket.io,您需要使用socket.io-client

var socket = require('socket.io-client')('https://website.com:3000');

socket.on('connect', function(){
    socket.emit('room', 'connecting');

    socket.on('message' , function (data){
        alert(data.message);
    });
});