connection copy.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // import { io } from "https://cdn.socket.io/4.4.1/socket.io.esm.min.js";
  2. export class Connection {
  3. constructor() {
  4. socket: null;
  5. client: null;
  6. userSig: null;
  7. }
  8. startSocket(config) {
  9. //192.168.0.52:9099/?userId=123
  10. console.log("init-startSocket");
  11. const { sdkAppId, roomId, userId } = config;
  12. this.socket = io("wss://demo-kms.4dage.com", {
  13. reconnectionDelayMax: 10000,
  14. transports: ["websocket"],
  15. query: {
  16. userId: userId,
  17. roomId: roomId,
  18. },
  19. });
  20. this.socket.on("connect", (data) => {
  21. console.log("111", data);
  22. });
  23. this.socket.on("connect_error", (error) => {
  24. console.log("error", error);
  25. });
  26. // setTimeout(() => {
  27. // console.log("this.socket", this.socket);
  28. // }, 3000);
  29. }
  30. async init(config, callback) {
  31. console.log("init-trtc");
  32. this.startSocket(config);
  33. const { sdkAppId, roomId, userId, role } = config;
  34. this.socket.on("getSign", async (data) => {
  35. const userSig = data;
  36. this.userSig = userSig;
  37. try {
  38. const client = TRTC.createClient({
  39. mode: "rtc",
  40. sdkAppId: sdkAppId,
  41. userId: userId,
  42. role: role,
  43. useStringRoomId: true,
  44. userSig,
  45. });
  46. this.client = client;
  47. callback();
  48. } catch (error) {
  49. console.log("joinRoom error", error);
  50. callback(error);
  51. }
  52. });
  53. }
  54. }