2012-09-30 85 views
0

在我的服務器上,我可以使用socket.io很好地運行我的節點服務器。但是,當我在localhost上嘗試幾乎完全相同的代碼時,我無法使其工作。本地主機上的Node.js(和socket.io)

client.js

$(function() { 

    /* Check for a <canvas> */ 
    if (! ("getContext" in document.createElement("canvas"))) { 
    alert("Sorry, it looks like your browser does not support canvas!"); 
    return false; 
    } 

    /* The URL of your web server and the port */ 
    //var url = "http://57o9.org:8081"; 
    var url = "localhost:8081"; 

    var doc = $(document); 
    var win = $(window); 
    var canvas = $("#canvas"); 
    var ctx = canvas[0].getContext("2d"); 

    /* Generate an unique ID based on time */ 
    var id = Math.round($.now() * Math.random()); 

    var drawing = false; 

    var clients = {}; 
    var cursors = {}; 
    var socket = io.connect(url); 

    socket.on("moving", function(data) { 
    /* Create a new cursor for new users */ 
    if (! (data.id in clients)) { 
     cursors[data.id] = $("<div class=\"cursor\">").appendTo("#cursors"); 
    } 

    /* Update mouse pointer data */ 
    cursors[data.id].css({ 
     "left" : data.x, 
     "top" : data.y 
    }); 

    /* Is the user drawing? */ 
    if (data.drawing && clients[data.id]) { 

     /* Draw a line on the canvas. clients[data.id] holds 
     the previous position of this user"s mouse pointer */ 

     drawLine(clients[data.id].x, clients[data.id].y, data.x, data.y); 
    } 

    /* Save the current client state */ 
    clients[data.id] = data; 
    clients[data.id].updated = $.now(); 
    }); 

    var prev = {}; 

    canvas.on("mousedown", function(e) { 
    e.preventDefault(); 
    drawing = true; 
    prev.x = e.pageX; 
    prev.y = e.pageY; 
    }); 

    doc.bind("mouseup mouseleave", function() { 
    drawing = false; 
    }); 

    var lastEmit = $.now(); 

    doc.on("mousemove",function(e) { 
    if ($.now() - lastEmit > 30) { 
     socket.emit("mousemove", { 
     "x": e.pageX, 
     "y": e.pageY, 
     "drawing": drawing, 
     "id": id 
     }); 

     lastEmit = $.now(); 
    } 

    /* Draw a line for the current user"s movement */ 
    if (drawing) { 
     drawLine(prev.x, prev.y, e.pageX, e.pageY); 

     prev.x = e.pageX; 
     prev.y = e.pageY; 
    } 
    }); 

    /* Remove inactive clients after 10 seconds of inactivity */ 
    setInterval(function() { 
    for (ident in clients) { 
     if ($.now() - clients[ident].updated > 10000) { 
     cursors[ident].remove(); 
     delete clients[ident]; 
     delete cursors[ident]; 
     } 
    } 

    }, 10000); 

    function drawLine(fromx, fromy, tox, toy) { 
    fromx -= canvas[0].offsetLeft; 
    tox -= canvas[0].offsetLeft; 
    fromy -= canvas[0].offsetTop; 
    toy -= canvas[0].offsetTop; 

    ctx.moveTo(fromx, fromy); 
    ctx.lineTo(tox, toy); 
    ctx.stroke(); 
    } 
}); 

server.js

/* Include libraries */ 
var app = require('http').createServer(handler); 
var io = require('socket.io').listen(app); 
io.set("log level", 1); // Disable socket.io logging 
var nodeStatic = require('node-static'); 

/* This will make all the files in the current folder accessible from the web */ 
var fileServer = new nodeStatic.Server("./"); 

/* Set up server port */ 
app.listen(8081); 

/* Check if the URL of the socket server is opened in a browser */ 
function handler(request, response) { 
    request.addListener("end", function() { 
    fileServer.serve(request, response); // Return the correct file 
    }); 
} 

/* Listen for incoming connections from clients */ 
io.sockets.on("connection", function (socket) { 
    socket.on("mousemove", function (data) { 
    socket.broadcast.emit("moving", data); 
    }); 
}); 

這是一個基本的協作繪圖程序。在我的服務器上它可以工作 - 我可以畫畫,其他人可以看到我繪製的內容,並且我們可以一起繪製。但是,在localhost:8081上不起作用。

我試着調試socket.io,每次我在localhost上打開一個新頁面時,我得到的唯一一條是:8081。

調試 - 提供靜態內容/socket.io.js

我的當前目錄下的樣子:

node_modules js index.html assets 

在Firefox極光,Web控制檯不給任何錯誤,但我鉻得到這個錯誤:

Failed to load resource http://0.0.31.145:8081/socket.io/1/?t=1349017485579 

我有socket.io安裝在~/node_modules/和項目目錄上。我正在運行節點服務器node js/server.js

任何想法?先謝謝你。

回答

0

錯誤在client.js上。 「localhost:8081」是不正確的,你必須寫「http:// localhost:8081」。