Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

As I understood it, from http://socket.io/#how-to-use, node.js automatically serves the socket.io file on the server.

I have installed socket.io with npm install socket.io and I can see that it resides in node_modules one level above the server root.

server.js:

    var static = require('./plugins/node-static');
var socketIO = require('socket.io');
var clientFiles = new static.Server('./client');

var http = require('http');
httpServer = http.createServer(function (request, response) {
    request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) { .....

index.html:

<html>
<head>
    <title>Chat</title>
</head>
<body>
    <script src="/socket.io/socket.io.js"></script>
    <script type="text/javascript"
            src="http://code.jquery.com/jquery-1.5.2.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            var webSocket = new io.Socket('localhost', { port: 8253 });
            webSocket.connect(); .......

Starting the server works fine, but when opening index.html, I receive the following error:

GET http://localhost:8253/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined                 :8253/:25

Ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
315 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try listening on the server after you bind it with socket.io

Place this

httpServer.listen(8253);

after

var webSocket = socketIO.listen(httpServer);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...