用Phoenix.js连接Angular 9
我正在尝试将Angular 9作为客户端连接到现有的phoenix频道 首先,我通过cli命令创建了angular应用程序,并通过npm install phoenix下载了phoenix。 然后我在用Phoenix.js连接Angular 9,angular,phoenix,angular9,phoenix-channels,Angular,Phoenix,Angular9,Phoenix Channels,我正在尝试将Angular 9作为客户端连接到现有的phoenix频道 首先,我通过cli命令创建了angular应用程序,并通过npm install phoenix下载了phoenix。 然后我在angular.json "scripts": [ "./node_modules/phoenix/priv/static/phoenix.js" ] 然后我创建了一个服务 import { Injectable } from '@angula
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错误类型错误:无法读取未定义的属性“connect”
在新的AppComponent上(app.component.ts:14)
phoenix.js:1到'wss://api.catalx.io/markets/websocket?vsn=2.0.0'失败:WebSocket握手期间出错:意外响应代码:403
我想我遇到这些错误是因为PhoenixService
无法抓取phoenix.js
?
很少的帮助是感激的 我想应该是这样 如果您使用“NPMI-S phoenix”安装js,则无需在“脚本”中添加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();
}
}