简体   繁体   English

如何使用Web套接字和角度CLI设置代理

[英]How to setup a proxy using web sockets and angular CLI

I have a simple web app built using the angular CLI. 我有一个使用角度CLI构建的简单Web应用程序。 I want it to communicate with a backend using web sockets. 我希望它使用Web套接字与后端通信。 I have the backend already written and have tested with a simple index.html page that the server can send and receive on sockets. 我已经编写了后端,并使用服务器可以在套接字上发送和接收的简单index.html页面进行测试。

In my angular-cli project I have setup a proxy config file to setup a proxy to the backend. 在我的angular-cli项目中,我设置了一个代理配置文件来设置后端的代理。

proxy.conf.json proxy.conf.json

{
  "/sock": {
    "target": "http://localhost:3000",
    "changeOrigin": true,
    "ws": true,
    "logLevel": "debug"
  }
}

Then start the server with the following. 然后使用以下命令启动服务器。

ng serve --proxy-config proxy.conf.json

For now I have a service that simply attempts to open a socket and send a fixed string which I'm expecting to see logged by the backend. 现在我有一个服务,只是尝试打开一个套接字并发送一个固定的字符串,我希望看到后端记录。

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';

@Injectable()
export class ChatService {

  private socket: any;

  constructor() {
    this.socket = io({ 'path': '/sock' });
    this.socket.emit('chat message', 'Hello World from browser!');
   }

}

Note: I've had several go's at this with and without the /sock part of the url. 注意:在有和没有url的/ sock部分的情况下,我已经进行了几次操作。

I start both servers. 我启动两台服务器。 Get no console errors in the browser. 在浏览器中没有控制台错误。 But in the angular CLI web pack server I get the following messages. 但在角度CLI Web包服务器中,我收到以下消息。

10% building modules 2/2 modules 0 active[HPM] Proxy created: /sock  ->  http://localhost:3000
[HPM] Subscribed to http-proxy events:  [ 'error', 'close' ]

[HPM] GET /sockjs-node/530/z1z3teld/websocket -> http://localhost:3000
[HPM] Upgrading to WebSocket
[HPM] Error occurred while trying to proxy request /sockjs-node/530/z1z3teld/websocket from localhost:4200 to http://localhost:3000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

Are web sockets supported or have I made a silly mistake? 是支持网络套接字还是我犯了一个愚蠢的错误? Thanks 谢谢

I managed to figure it out with a bit of trial and error. 我设法通过一些试验和错误弄明白了。 I looked at the console for the basic index.html page that works within the backend project. 我在控制台中查看了在后端项目中工作的基本index.html页面。 This backend project is basically the chat server demo application on the socket.io website. 这个后端项目基本上是socket.io网站上的聊天服务器演示应用程序。 I noticed that when it opens up the web socket the url looks like the following: 我注意到,当它打开Web套接字时,url看起来如下所示:

http://localhost:3000/socket.io/EIO=3&transport=websocket&sid=wTvdQTclHXJSUmAmAAAA

So back in the angular CLI project I modified my proxy config to include the /socket.io/ part plus also added a wildcard. 所以回到角度CLI项目中我修改了我的代理配置以包含/socket.io/部分加上还添加了一个通配符。

{
  "/sock/*": {
    "target": "http://localhost:3000/socket.io/",
    "ws": true,
    "logLevel": "debug"
  }
}

Bingo! 答对了! Now when the service is constructed it opens the socket and emits a message which I can see logged in the backend. 现在,当构建服务时,它会打开套接字并发出一条消息,我可以在后端看到该消息。

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

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