0

我正在尝试使用phoenix.jsAngular 9作为客户端连接到现有的 phoenix 通道。

首先,我通过 cli 命令创建了一个 Angular 应用程序,并通过npm install 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 错误类型错误:无法读取新 AppComponent 处未定义的属性“连接”(app.component.ts:14)

phoenix.js:1 WebSocket 连接到“wss://api.catalx.io/markets/websocket?vsn=2.0.0”失败:WebSocket 握手期间出错:意外响应代码:403

我想我遇到这些错误是因为PhoenixService无法抓取phoenix.js?一点帮助表示赞赏!

4

1 回答 1

1

我认为应该是这样的,如果您使用“npm i -S phoenix”安装它,则无需将 js 添加到“脚本”中

无法使用 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();
  }
}
于 2020-04-30T23:00:52.997 回答