我是网络套接字的新手,我用烧瓶套接字服务器和角套接字作为客户端来实现网络套接字服务器,
我在烧瓶里用了这个代码
application.py
@socketio.on('connect', namespace='/test')
def test_connect():
print('Client connected')我在角服务中使用这个代码连接到烧瓶服务器。
socket.service.ts
import { Injectable } from "@angular/core";
import * as io from "socket.io-client";
import { Observable } from "rxjs";
@Injectable({
providedIn: "root"
})
export class SocketService {
private socket;
constructor() {
this.socket = io("/test"); // i get example by connect using flask namespace
this.socket = io("localhost:5000"); // i tried this
this.socket = io(`/api`); // i also tried this
}
public sendMessage(message) {
this.socket.emit("new-message", message);
}
public getMessages = () => {
return Observable.create(observer => {
this.socket.on("new-message", message => {
observer.next(message);
});
});
};
}我还尝试使用localhost:5000作为URL连接到烧瓶服务器,但这导致了CORS问题,面对这个问题后,我还尝试使用下面proxy.config.json中的'/api‘作为url进行连接。
{
"/api": {
"target": "http://127.0.0.1:5000",
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}这是我的app.component
app.component.ts
import { Component } from "@angular/core";
import { SocketService } from "./socket.service";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
title = "angularsocketio";
constructor(private socketService: SocketService) {}
}https://stackoverflow.com/questions/57764878
复制相似问题