套接字IO未连接到正确的服务器
Socket IO not connecting to the right server.
我刚刚开始学习node,已经从互联网上拼凑出了一些零碎的东西。我想使用node的一件事是SendGrid从客户端发送电子邮件。我通过在socket.io客户端手动输入socket = io.connect(http://localhost:8080);
(就像大家说的那样)来完成这项工作。在我去heroku或类似的服务上主持之前,这在当地一直很好。
以下是我的服务器代码的样子(下面的函数无关紧要,因为它们只是运行sendgrid填充:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// This is the socket.io port. I want the client to go through this.
server.listen(8080);
// this is used to serve a static html page in the public folder.
// I feel like the way I have this set up is part of the issue
app.set('port', (5000));
app.use(express.static(__dirname + '/public'));
app.listen(app.get('port'), function() {
console.log('Node app is running on port ' + app.get('port'));
});
我去地址http://localhost:5000以查看服务器的html。
这是我编写的客户端代码:
// Init the socket
socket = io.connect('http://localhost');
// send stuff through it. this stuff doesnt matter. but it sends values
// Via json to the server to then use in an email.
socket.emit('sendCompile', {adr: receptients, bod: body });
这给了我一个错误:
socket.io-1.4.5.js:1 GET
http://localhost/socket.io/?
EIO=3&transport=polling&t=LFC_cxO
如有任何帮助,我们将不胜感激。(我已经为此工作了5个多小时:D)
附言:我已经试过像其他人想的那样将其更改为socket = io();
和socket = io.connect();
。但这两种方法都不起作用。我觉得这是我如何编写服务器的问题,因为我只是在找到它们时把它们拼凑在一起。
您需要将域从localhost更改为您的网站,并添加端口,例如:
socket = io.connect('http://localhost');
应该成为
socket = io.connect('http://mywebsite.com:8080');
还要注意,您用于套接字的端口需要打开——如果端口没有打开,您将遇到更多问题。
相关文章:
- 如何使用Socket.io将命令从客户端发送到服务器
- 可以't连接客户端和服务器import.io
- Socket.io socket.emit 不将数据传递到服务器
- io.sockets.mit未向服务器发送消息
- 在不同子域上使用Socket.IO服务器和客户端
- 'simple'套接字中的作用域问题.IO服务器
- "对象不是函数“;当将Node.js HTTP服务器对象传递给Socket.IO时
- 当用户刷新页面时,如何在单独的服务器中处理socket.io上的会话
- Nitrous.io上Meteor应用程序的服务器端调试
- Socket.IO:重新连接导致服务器连接代码运行两次
- Socket.IO:如何在同一服务器中的Socket.on()事件之间通信/交换数据
- 套接字IO未连接到正确的服务器
- 服务器到服务器套接字IO消息
- socket.io 客户端,不会向服务器发出消息
- 节点.js socket.io 网络服务器提供索引.html以外的其他文件
- 套接字 IO 未使用 swift 连接到服务器
- socket.io (nodejs) 是否通过在回调完成期间阻止服务器来影响服务器性能
- socket.io 客户端 - 如果没有连接或服务器关闭,则不会触发发出回调
- 404 GET 在 Django 模板中的服务器 socket.io
- 向 Socket.IO 服务器发出事件