最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebSocket not connecting to socket.io - Stack Overflow

programmeradmin2浏览0评论

I am using Node.js with socket.io to implement websockets in one of my pages. server.js (what Node.js runs) has this code:

var http = require("http").createServer(),
io = require("socket.io").listen(http);

http.listen(8080);

io.sockets.on("connection", function(socket) {
        socket.emit("message", {hello:"world"});
});

And this is the code I'm trying to connect with:

var socket = new WebSocket("ws://92.60.122.235:8080/");
socket.onopen = function() {  
    alert("Socket has been opened!");  
}

When I load the page, nothing happens. I'm using Chrome, and I know websockets are supported. No errors are present in the error console, and if I watch socket.io serving requests from mand line I don't see any user connecting.

As far as I know this should work, could anyone explain what could be going wrong?

I am using Node.js with socket.io to implement websockets in one of my pages. server.js (what Node.js runs) has this code:

var http = require("http").createServer(),
io = require("socket.io").listen(http);

http.listen(8080);

io.sockets.on("connection", function(socket) {
        socket.emit("message", {hello:"world"});
});

And this is the code I'm trying to connect with:

var socket = new WebSocket("ws://92.60.122.235:8080/");
socket.onopen = function() {  
    alert("Socket has been opened!");  
}

When I load the page, nothing happens. I'm using Chrome, and I know websockets are supported. No errors are present in the error console, and if I watch socket.io serving requests from mand line I don't see any user connecting.

As far as I know this should work, could anyone explain what could be going wrong?

Share Improve this question edited Dec 13, 2017 at 14:54 wonea 4,98917 gold badges91 silver badges143 bronze badges asked Nov 13, 2011 at 1:16 James DawsonJames Dawson 5,40920 gold badges75 silver badges129 bronze badges 4
  • 1 Why aren't you using socket.io client? I believe they have some authentication, so this is not supposed to work. – Farid Nouri Neshat Commented Nov 13, 2011 at 1:21
  • Ah, wasn't aware I needed to use the client aswell. I'll try and get back to you. – James Dawson Commented Nov 13, 2011 at 1:22
  • Alright, added the socket.io in a script tag above all my other scripts, and it still doesn't work. Same problem as before. I have checked if the server is actually serving the socket.io client script and it is. – James Dawson Commented Nov 13, 2011 at 1:27
  • Well you don't use web sockets directly. See my answer. You use io.connect(...) to connect to the server, socket.on(...) to add a listener, and socket.emit(...) to send something. Socket.on doesn't add on the native objects! It has its own! – Farid Nouri Neshat Commented Nov 13, 2011 at 1:32
Add a ment  | 

2 Answers 2

Reset to default 7

You need a socket.io client to pass some authentication phases I believe. Try this, and it should work(the client javascript is served by socket.io itself, don't worry about it).

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('message', function (data) {
    console.log(data);
    socket.emit('helloworld', { msg: 'why do you so love to say hello world?' });
  });
</script>

From http://socket.io/#faq

Why not just call it WebSocket if the actual WebSocket is not present and mimick its API?

Socket.IO does more than WebSocket, even if WebSocket is selected as the transport and the user is browsing your website with an ultra modern browser. Certain features like heartbeats, timeouts and disconnection support are vital to realtime applications but are not provided by the WebSocket API out of the box.

This is akin to jQuery's decision of creating a feature-rich and simple $.ajax API as opposed to normalizing XMLHttpRequest.

You can download the webpage source code that runs in Chrome, Firefox, and IE (at least) via the blog article "Websocket Server Demonstration" from the High Level Logic Project. The webpage is set up for developers.

发布评论

评论列表(0)

  1. 暂无评论