本文介绍了Socket.io不由Node.js服务器提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,从,节点.js自动提供服务器上的socket.io文件。

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

我已经安装了socket.io,其中包含 npm install socket.io 我可以看到它位于服务器根目录上一级的 node_modules 中。

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:

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:

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(); .......

开始服务器工作正常,但在打开index.html时,我收到以下错误:

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

想法?

推荐答案

用socket.io绑定后尝试在服务器上监听

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

放置此

httpServer.listen(8253);
var webSocket = socketIO.listen(httpServer);

这篇关于Socket.io不由Node.js服务器提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:56