javascript - 未找到套接字 IO (404)

标签 javascript node.js sockets websocket socket.io

我在让套接字 io 工作超出其教程中的示例时遇到一些问题。

问题是当我想从本地 apache 服务器上的网站调用 chatDiv.html 时。

我的 Node js 服务器名为 index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(function (req, res, next) {

// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost');

// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);

// Pass to next layer of middleware
next();
});

app.get('/', function(req, res){
  res.sendFile(__dirname + '/chatDiv.html');
});

io.on('connection', function(socket){
console.log('a user connected');
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log(msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');

服务器加载的客户端文件是chatDiv.html

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<div class="chat body">
<ul style="width:100%;height:100px;" id="messages"></ul>

<input style="width:100px;height:20px"id="m" autocomplete="off">  </input><button id="sendM"   >Send</button>
    
</div>

最后,我想与我的 apache 服务器上的套接字 io 进行交互。我通过将 chatDiv.html 的内容加载到容器中来实现此目的,如下所示

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
$( document ).ready(function() {
  $("#chatContainer").load("http://localhost:3000/", function() {
    console.log('loaded chatdiv');
    var socket = io();
    $( "#chatContainer" ).on( "click", "#sendM", function() {
      console.log('clicked');
    }); 
  }); 
});

现在,chatDiv 的内容正在加载到我的网站,我可以看到正在加载套接字 io 文件。但调用时出现错误 var 套接字 = io();

GET http://localhost/socket.io/?EIO=3&transport=polling&t=L8zL8x0 404 (Not Found)

最佳答案

您能具体说明您遇到的错误吗?您可能想在 io() 周围包裹一个 try-catch block ,看看它是否给您任何提示。我怀疑这与您的端口访问权限或防火墙设置有关。

示例:

$( document ).ready(function() {
    $("#chatContainer").load("http://localhost:3000/", function() {
        console.log('loaded chatdiv');
        try{
           var socket = io();
        }
        catch(e){
           //log the error here
           console.log(e);
        }
        $( "#chatContainer" ).on( "click", "#sendM", function() {
            console.log('clicked');
        }); 
    }); 
});

关于javascript - 未找到套接字 IO (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779088/

相关文章:

mysql - 存储在 MySQL 中的二进制数据被 Node 损坏

javascript - 如何将node与mysql一起用于react.js前端?

python - 我的套接字程序出现连接错误

python - 创建持续不断的变化提要

javascript - ajax调用WCF服务(跨域)

javascript - 在 Firebase 中写入数据后读取数据

javascript - 在所有元素上触发的主干事件监听器

node.js - 是否有用于从 node.js 中的命令行获取用户输入的模块?

c++ - 我应该处理 WSASend() 可能不会发送所有数据的事实吗?

javascript - 是否可以用 React 替换 DOM 元素?