Socket.io没有响应,Node.js

时间:2014-07-14 05:46:40

标签: mysql node.js socket.io localhost backend

我正在将代码移到服务器上。此代码在我在localhost上设置的服务器上工作并完美呈现数据库信息,但是当我从服务器运行代码时,会显示index.html中出现“io未定义”的错误。无论出于何种原因,socket.io都无法识别。此外,如果我在浏览器中键入localhost:3000,则不显示任何内容。任何帮助将不胜感激。

我有两个文件,server.js和index.html。

server.js:

    var mysql = require('mysql')
    var io = require('socket.io').listen(3000)
    var db = mysql.createConnection({
    host: '127.0.0.1', // Important to connect to localhost after connecting via ssh in screen
    user: 'username',
    password: '12345',
    database: '12345',
    port: 3306
})


// Log any errors connected to the db
db.connect(function(err){
    if (err) console.log(err)
})


// Define/initialize our global vars
var notes = []
var isInitNotes = false
var socketCount = 0



//Socket.io code below

io.sockets.on('connection', function(socket){
    // Socket has connected, increase socket count
    socketCount++
    // Let all sockets know how many are connected

    io.sockets.emit('users connected', socketCount)

    socket.on('disconnect', function() {
        // Decrease the socket count on a disconnect, emit
        socketCount--
        io.sockets.emit('users connected', socketCount)
    })
    socket.on('new note', function(data){

        // New note added, push to all sockets and insert into db
        notes.push(data)
        io.sockets.emit('new note', data)
        // Use node's db injection format to filter incoming data
        db.query('INSERT INTO notes (note) VALUES (?)', data.note)
    })
 console.log("10");

    // Check to see if initial query/notes are set
    if (! isInitNotes) {
        // Initial app start, run db query
        db.query('SELECT * FROM `Users`')
            .on('result', function(data){
                // Push results onto the notes array
                //console.log(notes);
                notes.push(data)
            })
            .on('end', function(){
                // Only emit notes after query has been completed
                socket.emit('initial notes', notes)
            })

        isInitNotes = true
    } else {
        // Initial notes already exist, send out
        socket.emit('initial notes', notes)
    }

})

index.html :(认为问题出在我连接socket.io.js文件的方式,或者在我声明变量“socket”的代码行中)

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <!-- Script below works with my server I set up on local host
    <script src="http://localhost:3000/socket.io/socket.io.js"></script>-->

    <!-- script below properly links to the socket.io.js file in my directory, and throws no errors-->
    <script type= "node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>

        <script>
        $(document).ready(function(){
            var socket = io.connect('http://localhost:3000'); //LINE OF CODE IN QUESTION




        //Code below not really relevant to problem, but still part of my project.

        socket.on('initial notes', function(data){
            var html = ''
            for (var i = 0; i < data.length; i++){
                // We store html as a var then add to DOM after for efficiency
                html += '<li>' + data[i].Name + '</li>'
            }
            $('#notes').html(html)
        })

        // New note emitted, add it to our list of current notes
        socket.on('new note', function(data){
            $('#notes').append('<li>' + data.Name + '</li>')
        })

        // New socket connected, display new count on page
        socket.on('users connected', function(data){
            $('#usersConnected').html('Users connected: ' + data)
        })

        // Add a new (random) note, emit to server to let others know
        $('#newNote').click(function(){
            var newNote = 'This is a random ' + (Math.floor(Math.random() * 100) + 1)  + ' note'
            socket.emit('new note', {note: newNote})
        })
    })
    </script>
    <ul id="notes"></ul>
    <div id="usersConnected"></div>
    <div id="newNote">Create a new note:</div

解决!

想出来。我使用“script src =”我的服务器ip地址:3000 / socket.io / socket.io.js“然后将变量socket更改为var socket = io.connect('Servers ip address:3000');那么答案是要把localhost一起拿走。

1 个答案:

答案 0 :(得分:0)

我相信从服务器加载Socket.io可能会出现问题,如果它在本地运行(可能是权限问题?),请尝试从Socket.io CDN加载它进行测试。

<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>