简体   繁体   English

将socket.io客户端指向heroku服务

[英]Pointing socket.io client to heroku service

I have used socket.io, Node.JS, and express to create a real-time chat application that I could view by creating a local server. 我使用过socket.io,Node.JS和express来创建一个实时聊天应用程序,可以通过创建本地服务器来查看它。 However, instead of using a local server, I would like to point my client to an existent heroku service. 但是,我不想使用本地服务器,而是希望将客户端指向现有的heroku服务。 How do I go about doing this? 我该怎么做呢?

When I do the following, 当我执行以下操作时,

var spot = io("https://spotim-demo-chat-server.herokuapp.com");

all functions relating to connecting, disconnecting, username registration, and messaging seem to fail. 与连接,断开连接,用户名注册和消息传递有关的所有功能似乎都失败了。 They do not function in the chat nor do their console.log messages appear in terminal. 它们在聊天中不起作用,其console.log消息也不会出现在终端中。

I have also considered creating a HTTP server and having it listen to the port and IP address of the heroku service with .listen(). 我还考虑过创建一个HTTP服务器,并使其通过.listen()监听heroku服务的端口和IP地址。 However for my assignment, I was not provided with either of these values, simply the heroku url. 但是,对于我的作业,没有为我提供任何一个值,仅是heroku url。

I'm not sure where you're having trouble without more code from you. 我不确定如果没有更多代码,您在哪里遇到麻烦。 I've setup a basic socket.io client as follows and can connect without issue. 我已经按照以下步骤设置了一个基本的socket.io客户端,并且可以正常连接。 You can run it with the following to see just the client's messages DEBUG=spot-client node client.js or if you wish to see all the debug info from the socket client itself as well DEBUG=* node client.js . 您可以使用以下命令运行它,以仅查看客户端消息DEBUG=spot-client node client.js或者如果您希望查看套接字客户端本身的所有调试信息以及DEBUG=* node client.js The latter might help you diagnose the issue further. 后者可以帮助您进一步诊断问题。 Also be sure to install the socket.io-client and debug packages. 另外,请确保安装socket.io-clientdebug软件包。 Hope this helps! 希望这可以帮助!

const io = require('socket.io-client');
const debug = require('debug')('spot-client');
var spot = io("https://spotim-demo-chat-server.herokuapp.com");

spot.on('connect', function(){
    debug("connected");
});
spot.on('event', function(data){
    debug("event",data);
});
spot.on('disconnect', function(){
    debug("disconnect");
});

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

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