用 Phoenix.js 连接 Angular 9

Wire up Angular 9 with Phoenix.js

我正在尝试将 Angular 9 作为客户端连接到 phoenix.js 以连接到现有的 phoenix 频道。

首先,我通过 cli 命令创建了一个 angular 应用程序,并通过 npm install phoenix 下载了 phoenix。 然后我在 angular.json

中添加了凤凰路径
 "scripts": [
              "./node_modules/phoenix/priv/static/phoenix.js"
            ]

然后我创建了一个服务

import { Injectable } from '@angular/core';
import { Phoenix } from 'phoenix';

declare var Socket: any;  // there is no typescript version of the package available so we cannot use a compile time import
declare var Phoenix: any;

@Injectable({
  providedIn: 'root'
})
export class PhoenixService {

  socket: any;

  constructor() {
    let socket = new Phoenix.Socket("wss://api.catalx.io/markets", {params: {}})
    socket.connect()

    let channel = socket.channel("updates:CAD-BTC", {})
    channel.on("new_msg", msg => console.log("Got message", msg) )
    channel.join()
      .receive("ok", ({messages}) => console.log("catching up", messages) )
      .receive("error", ({reason}) => console.log("failed join", reason) )
      .receive("timeout", () => console.log("Networking issue. Still waiting..."))
    channel.push("fetch", "market_state")
  }
}

最后,在AppComponent

中调用了这个服务
export class AppComponent {

  constructor(private phoenixService: PhoenixService) {
    phoenixService.socket.connect();
  }
}

结果我得到了

core.js:6237 ERROR TypeError: Cannot read property 'connect' of undefined at new AppComponent (app.component.ts:14)

phoenix.js:1 WebSocket connection to 'wss://api.catalx.io/markets/websocket?vsn=2.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

我想我遇到这些错误是因为 PhoenixService 无法抓取 phoenix.js? 不胜感激!

我觉得应该是这样的 如果您使用 "npm i -S phoenix"

安装它,则无需将 js 添加到 "scripts"

无法使用 catalx 对其进行测试

import { Injectable } from '@angular/core';
import { Socket as PhoenixSocket } from 'phoenix';

export const WEBSOCKET_SERVER_URI = 'wss://api.catalx.io/markets';

@Injectable({
  providedIn: 'root'
})
export class PhoenixService {
  phoenixSocket: PhoenixSocket;

  constructor() {
    this.phoenixSocket = new PhoenixSocket(
      WEBSOCKET_SERVER_URI,
      {
        params: {},
      }
    );
    this.phoenixSocket.connect();
  }
}