使用带有WebSocket的ngrok(或socket.io)

wbrvyc0a  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(208)

Are there any simple sample code showing ngrok forwarding socket.io/websocket (running under nodejs on localhost)?
In other words, does

ngrok http 3000

work with a nodejs server and socket.io running on port 3000? Or something like

ngrok http+tcp 3000 (just wild guessing)

EDIT:
to answer my own question after help from ngrok.com
simply use

ngrok http 3000

You will see the web address string that localhost binds to. So in the client javascript code, change

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

to

var socket = io.connect('http://94349fe6.ngrok.io');

NOTE:
http://94349fe6.ngrok.io is just that web address string. Yours will be different from this one.
EDIT AGAIN:
actually, if one simply does:

var socket = io();

"it defaults to trying to connect to the host that serves the page"
So it works as well.

rryofs0p

rryofs0p1#

是的。如果你的节点应用程序在3000端口工作,就像你的例子一样,那么就用ngrok来创建一个反向代理,把3000端口公开给外界。Websockets可以很好地与它一起工作。

00jrzges

00jrzges2#

尝试使用LocalTunnel:

Usage: lt --port [num] <options>

https://www.npmjs.com/package/localtunnel

相关问题