繁体   English   中英

套接字IO未连接到正确的服务器。

[英]Socket IO not connecting to the right server.

我才刚刚开始学习节点,并从互联网周围拼凑了一些零碎的东西。 我想使用node的一件事是SendGrid从客户端发送电子邮件。 我通过手动输入socket = io.connect(http://localhost:8080); (就像每个人都说的那样)在socket.io客户端上。 在本地运行良好,直到我将其托管在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)

PS:我已经尝试过将其更改为socket = io(); socket = io.connect(); 像其他所有人似乎都认为这样做。 但这些都不起作用。 我觉得这是我编写服务器的方式的问题,因为我只是将发现的东西拼凑在一起。

您需要将域名从localhost更改为您的网站,然后添加端口,例如:

socket = io.connect('http://localhost');

应该成为

socket = io.connect('http://mywebsite.com:8080');

还要注意,用于套接字的端口需要打开-如果端口未打开,则会遇到更多问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM