Why is my emit not getting called?

Posted by cRaZiRiCaN on Stack Overflow See other posts from Stack Overflow or by cRaZiRiCaN
Published on 2012-11-11T18:24:57Z Indexed on 2012/12/15 17:04 UTC
Read the original article Hit count: 179

Filed under:
|

The client and server connect just fine. For some reason the emit on my client is not firing correctly. I am trying to get the testEmit and testEmit2 working.

This is my server:

express = require 'express'
mongo = require 'mongodb'
app = express()
server = (require 'http').createServer(app)
io = (require 'socket.io').listen(server)

server.listen(8080)

app.use(express.static(__dirname + '/public'))

# db = new mongo.Db("documentsdb", new mongo.Server("localhost", 27017, auto_reconnect: true), {safe:true})

io.sockets.on 'connection', (socket) ->
    console.log 'Socket.io is connected!'
    #This returns an array of documents sorted via date by decreasing order. (Most recent documents first.)
    socket.on 'loadRecentDocuments', ->
        console.log 'Loading most recent documents.'
        db.collection 'documents', (err, collection) ->
            collection.find().sort(dateAdded: -1).toArray (err, documents) ->
                #This emit is recieved at index.html where a javascript function sendDocuments manages the documents.
                socket.emit 'sendDocuments', documents
                return

    #The index.html provides the code data from the search box via a javascript.
    io.sockets.on 'findDocuments', (code) ->
        #Returns an array of documents with the corresponding class code.
        documentCodeToSearch = code
        console.log 'Retreaving documents with code: ' + documentCodeToSearch
        db.collection 'documents', (err, collection) ->
            collection.find(code:documentCodeToSearch).toArray (err, documents) ->
                socket.emit 'sendDocuments', documents
                return

    #Uploads a document to the server. documentData is sent via javascript from submit.html
    io.sockets.on 'addDocument', (documentData) ->
        console.log 'Adding document: ' + documentData
        db.collection 'documents', (err, collection) ->
            collection.insert documentData, safe: true
            return

    #Test socket.io
    io.sockets.on 'testEmit', ->
        console.log('Emit recieved.')
        socket.emit 'testEmit2', 'caca'
        return

app.listen 1337
console.log "Listening on port 1337..."

This is my client:

<!doctype HTML>
<html>
<head>
    <title>ProjectShare</title>
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script>
        //Make sure DOM is ready before mucking around.
        $(document).ready(function()
        {
            console.log('jQuery entered!');
            var socket = io.connect('http://localhost:8080');   
            socket.emit('testEmit');
            socket.on('testEmit2', function(data)
            {
                console.log('Emit recieved at browser.');
                console.log(data);
            });
            console.log('jQuery exit.');
        });
    </script>
</head>
<body>
    <ol>
        <li><a href="index.html">ProjectShare</a></li>
        <li><a href="guidelines.html">Guidelines</a></li>
        <li><a href="upload.html">Upload</a></li>
        <li>
            <form>
                <input type = "search" placeholder = "enter class code"/>
                <input type = "submit" value = "Go"/>
            </form>
        </li>
    </ol>
    <ol id = "documentList">
    </ol>
</body>
</html>

© Stack Overflow or respective owner

Related posts about coffeescript

Related posts about socket.io