本文介绍了使用Socket.io将客户端连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对node.js和它的插件都比较新,所以这可能是一个初学者问题。

I'm relatively new to node.js and it's addons, so this is probably a beginnersquestion.

我正试图在一个简单的HTML页面上webserver使用websocket.io连接到运行node.js的其他服务器。

I'm trying to get a simple HTML page on a webserver connect to a different server running node.js with websocket.io.

我的代码如下所示:

客户

<script src="socket.io/socket.io.js"></script>
<script>
    // Create SocketIO instance, connect

    var socket = new io.Socket();

    socket.connect('http://127.0.0.1:8080');

    // Add a connect listener
    socket.on('connect',function() {
      console.log('Client has connected to the server!');
    });
    // Add a connect listener
    socket.on('message',function(data) {
      console.log('Received a message from the server!',data);
    });
    // Add a disconnect listener
    socket.on('disconnect',function() {
      console.log('The client has disconnected!');
    });

    // Sends a message to the server via sockets
    function sendMessageToServer(message) {
      socket.send(message);
    };
</script>

Serverside

// Require HTTP module (to start server) and Socket.IO
var http = require('http');
var io = require('socket.io');
var port = 8080;

// Start the server at port 8080
var server = http.createServer(function(req, res){
    // Send HTML headers and message
    res.writeHead(200,{ 'Content-Type': 'text/html' });
    res.end('<h1>Hello Socket Lover!</h1>');
});

server.listen(port);

// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);

// Add a connect listener
socket.on('connection', function(client){
    console.log('Connection to client established');

    // Success!  Now listen to messages to be received
    client.on('message',function(event){
        console.log('Received message from client!',event);
    });

    client.on('disconnect',function(){
        clearInterval(interval);
        console.log('Server has disconnected');
    });
});

console.log('Server running at http://127.0.0.1:' + port + '/');

启动服务器正常运行也可以正常工作,按预期返回Hello Socket Lover。但我想与套接字进行不同的页面通话,而不是从node.js运行一个。

Starting up the server works fine and running http://localhost:8080 in my browser also works, returning 'Hello Socket Lover' as expected. But I want to make a different page talk to the sockets, not run one from node.js.

但是当我运行它时,没有任何反应,Chrome控制台会返回:

But when I run it, nothing happens and the Chrome console returns:

Failed to load resource            http://undefined/socket.io/1/?t=1333119551736
Failed to load resource            http://undefined/socket.io/1/?t=1333119551735

我整天都在这。有什么帮助吗?

I've been at this all day. Any help?

推荐答案

您是否尝试过不是从相对URL加载socket.io脚本?

Have you tried loading the socket.io script not from a relative URL?

您正在使用:

<script src="socket.io/socket.io.js"></script>

并且:

socket.connect('http://127.0.0.1:8080');

您应该尝试:

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

并且:

socket.connect('http://localhost:8080');

切换 localhost:8080 当前设置。

此外,根据您的设置,从其他域(同源策略)加载客户端页面时,您可能会遇到一些与服务器通信的问题。这可以用不同的方式克服(在这个答案的范围之外,google / SO它)。

Also, depending on your setup, you may have some issues communicating to the server when loading the client page from a different domain (same-origin policy). This can be overcome in different ways (outside of the scope of this answer, google/SO it).

这篇关于使用Socket.io将客户端连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:52