我正在尝试使用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
?一点帮助表示赞赏!