Socket.io 在 URL 中调用 http://undefined/....
我从 socket.io 开始,举一个简单的例子:
客户端:
$(document).ready(function () {
var sock = new io.Socket();
sock.on('message', function (data) {
var obj = JSON.parse(data);
if(obj.message) {
$('#message').text(obj.message);
} else {
$('#timestamp').text(obj.timestamp);
$('#clients').text(obj.clients); } });
sock.connect('http://127.0.0.1:8333');
$("#poke").click(function() {
sock.send("Poke !");
});
});
服务器端:
var io = require('socket.io');var express = require('express');
var app = express.createServer();
app.configure(function(){
app.use(express.static(__dirname + '/public'));});
app.get('/', function(req, res, next){ res.render('./public/index.html');});
app.listen(8333);
var socket = io.listen(app, {
flashPolicyServer: false,
transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']});
var allClients = 0;
var clientId = 1;
socket.on('connection', function(client) { var my_timer; var my_client = { "id": clientId, "obj": client };
clientId += 1;
allClients += 1;
my_timer = setInterval(function () {
my_client.obj.send(JSON.stringify({ "timestamp": (new Date()).getTime(), "clients": allClients })); }, 1000);
client.on('message', function(data) { my_client.obj.broadcast(JSON.stringify({ message: "poke send by client "+my_client.id }));
console.log(data);
});
client.on('disconnect', function() {
clearTimeout(my_timer);
allClients -= 1;
console.log('disconnect'); });});
我的问题是客户端试图获取:
http://undefined/socket.io/1/?t=1310142283769&jsonp=0
这是未定义的。当我使用 http://localhost/socket.io/1/?t=1310142283769&jsonp=1 卷曲我的应用程序时,我得到了一些东西。
为什么套接字调用未定义,而一切似乎都设置正确。
如果你能帮助我的话谢谢你!
I'm starting with socket.io with a simple example:
Client Side:
$(document).ready(function () {
var sock = new io.Socket();
sock.on('message', function (data) {
var obj = JSON.parse(data);
if(obj.message) {
$('#message').text(obj.message);
} else {
$('#timestamp').text(obj.timestamp);
$('#clients').text(obj.clients); } });
sock.connect('http://127.0.0.1:8333');
$("#poke").click(function() {
sock.send("Poke !");
});
});
Server Side:
var io = require('socket.io');var express = require('express');
var app = express.createServer();
app.configure(function(){
app.use(express.static(__dirname + '/public'));});
app.get('/', function(req, res, next){ res.render('./public/index.html');});
app.listen(8333);
var socket = io.listen(app, {
flashPolicyServer: false,
transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']});
var allClients = 0;
var clientId = 1;
socket.on('connection', function(client) { var my_timer; var my_client = { "id": clientId, "obj": client };
clientId += 1;
allClients += 1;
my_timer = setInterval(function () {
my_client.obj.send(JSON.stringify({ "timestamp": (new Date()).getTime(), "clients": allClients })); }, 1000);
client.on('message', function(data) { my_client.obj.broadcast(JSON.stringify({ message: "poke send by client "+my_client.id }));
console.log(data);
});
client.on('disconnect', function() {
clearTimeout(my_timer);
allClients -= 1;
console.log('disconnect'); });});
My problem is that the client is trying to get:
http://undefined/socket.io/1/?t=1310142283769&jsonp=0
Which is undefined. When I curl my app with http://localhost/socket.io/1/?t=1310142283769&jsonp=1 I get something.
Why socket is calling undefined whereas everything seems to be set correctly.
Thank you if you can help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您在客户端中错误地创建了套接字。你应该有更多类似的东西:
并完全删除 sock.connect 行。
您应该查看 SocketIO 网站上的示例。 http://socket.io/#how-to-use
It looks like you are incorrectly creating the socket in your client. You should have something more like:
and remove the sock.connect line entirely.
You should look over the examples on the SocketIO website. http://socket.io/#how-to-use
我刚刚遇到同样的事情。对我来说,是针对 Socket.IO v0.6 编写的代码导致了这个问题——自从我上次使用 Socket.IO 以来,它已经更新到 v0.7 了!这里有一个迁移指南:
https://github.com /LearnBoost/Socket.IO/wiki/Migration-0.6-to-0.7+
I just encountered the same thing. For me, it was code written against Socket.IO v0.6 causing the issue—since I had last worked with Socket.IO it has updated to v0.7! There's a migration guide available here:
https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7+