-1

客户端和服务器连接得很好。由于某种原因,我的客户端上的发射没有正确触发。我正在尝试让 testEmit 和 testEmit2 正常工作。

这是我的服务器:

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..."

这是我的客户:

<!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>
4

1 回答 1

1

尝试将 io.sockets 更改为 socket on

io.sockets.on 'findDocuments', (code) ->

&&

 io.sockets.on 'testEmit', ->

这应该够了吧

于 2012-11-11T21:56:53.747 回答