套接字io客户端连接失败

时间:2015-07-02 07:59:47

标签: node.js socket.io

我正在尝试为客户端服务器通信编写一个简单的代码,我的服务器启动良好但是当我启动我的客户端时它会抛出错误,我在下面发布了我的代码请帮助解决这个问题。

server.js

 express = require('express');  
    var app = express();  
    var server = require('http').createServer(app);  
    var io = require('socket.io')(server);
    io.set('transports', ['xhr-polling']);

    app.use(express.static(__dirname + '/bower_components'));  
    app.get('/', function(req, res,next) {  
        res.sendFile(__dirname + '/index.html');
    });

    io.on('connection', function(client) {  
        console.log('Client connected...');

        client.on('join', function(data) {
            console.log(data);
        });

    });

    server.listen(8080,function() {
    console.log("server conected");
});  

的index.html

<!doctype html>  
<html lang="en">  
    <head>
        <script src="Server/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script> 
        <script> 

            var socket = io.connect('http://localhost:8080',{
                  'sync disconnect on unload': true
                });
            socket.on('connect', function(data) {
                socket.emit('join', 'Hello World from client');
            });
        </script>
    </head>
    <body>
        <h1>Hello World!</h1>
        <div id="future"></div>
        <form id="form" id="chat_form">
            <input id="chat_input" type="text">
            <input type="submit" value="Send">
        </form>                        
    </body>
</html>  

output screen

0 个答案:

没有答案