black11black
V2EX  ›  问与答

vue 前端如何优雅地获取 host 域名?

  •  
  •   black11black · Mar 7, 2020 · 7942 views
    This topic created in 2353 days ago, the information mentioned may be changed or developed.

    如题,要进行 websocket 连接,前端一如既往各种踩坑,

    ws 有一个问题就是由 ws 开头, 比如普通网址/index可以代表host:port/index,不管我服务挂在哪个 IP 哪个端口哪个域名他都会自动搞定,ws 怎么办呢?我现在只能写死成比如const url = "ws://127.0.0.1:8090/ws",感觉很菜啊

    感谢感谢

    2 replies    2020-03-07 22:28:42 +08:00
    shakaraka
        1
    shakaraka  
       Mar 7, 2020
    ws://127.0.0.1:8090 不就行了
    1KN6sAqR0a57no6s
        2
    1KN6sAqR0a57no6s  
       Mar 7, 2020
    const hostname = document.location.hostname
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3527 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 05:02 · PVG 13:02 · LAX 22:02 · JFK 01:02
    ♥ Do have faith in what you're doing.