我正在尝试将Angular 9
作为客户端与phoenix.js连接起来,以连接到现有的凤凰频道。
[首先,我通过cli命令创建了一个角度应用程序,然后通过npm install phoenix
下载了phoenix。然后我在angular.json
中添加了Phoenix路径
"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错误TypeError:无法读取未定义的属性'connect'在新的AppComponent(app.component.ts:14)上]
phoenix.js:1与'wss://api.catalx.io/markets/websocket?vsn = 2.0.0'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:403
我想我遇到这些错误是因为
PhoenixService
无法抓住phoenix.js
?感谢您的帮助!
我正在尝试通过phoenix.js将Angular 9作为客户端进行连接,以连接到现有的phoenix通道。首先,我通过cli命令创建了一个角度应用程序,并通过npm install phoenix下载了phoenix。 ...
我认为应该是这样的如果您使用“ npm i -S phoenix”进行安装,则无需在“脚本”中添加js]