簡體   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