【发布时间】:2014-11-21 18:05:03
【问题描述】:
我在这样的网址上得到 404:http://localhost.dev/socket.io/?EIO=3&transport=polling&t=1411749802784-54
SOCKET IO 版本: 1.x
APP.JS
var express = require("express"),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
server.listen(8080);
app.get('/', function(req,res){
console.log('started');
});
//Receive message
io.sockets.on('connection',function(socket){
socket.on('send message',function(data){
//Send message out
io.sockets.emit('new message',data);
});
});
所以我的网站在port 80 上运行,nodejs 在端口8080 上运行,我包含这样的socket.io js 文件:<script src="http://localhost.dev:8080/socket.io/socket.io.js"></script>,我可以在新选项卡中打开socket.io.js 文件,但是我猜这和新版本有关,我刚刚开始使用node。
//LE
$(document).ready(function(){
//Socket.io
var socket = io.connect();
//Form
var $messageForm = $('#chatForm');
var $messageFormInput = $messageForm.find('#messageText');
var $chat = $('.chat');
//Bind submit form
$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message',$messageFormInput.val());
//Clear input
$messageFormInput.val('');
});
//Receive message
socket.on('new message',function(data){
$chat.append(data);
});
});
有谁知道我该如何解决这个问题?
【问题讨论】:
-
如果您在
http://localhost.dev/socket.io/?EIO=3&transport=polling&t=1411749802784-54上的控制台中收到 404 错误,那么您可能没有在页面中正确添加来自 8080 端口的脚本。 url 不是http://localhost.dev:8080/socket.io/的事实是一个很好的指标,表明 url 正在发生一些时髦的事情,而不是 node/socket.io -
如您所见,如果您查看第一个 404 url,它不会在端口
:8080上发出请求 -
您是否仔细检查了您的 HTML 源代码(通过查看源代码或使用开发人员控制台工具进行检查)以确保浏览器正在读取的脚本标签确实包含 8080?如果脚本标签显示 8080,则请求应该是 8080。要么是那个,要么你的浏览器中有一些插件导致它忽略端口。浏览器不会自行忽略 url 上的 8080 端口。
-
是的,我确实检查了 devtools 并查看了源代码
-
您的
io.connect()代码在哪里?那看起来像什么?
标签: javascript html node.js socket.io