connection.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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() {
  9. //192.168.0.52:9099/?userId=123
  10. this.socket = io("ws://192.168.0.152:9099", {
  11. reconnectionDelayMax: 10000,
  12. transports: ["websocket"],
  13. query: {
  14. userId: "123",
  15. roomId: "8888",
  16. },
  17. });
  18. this.socket.on("connect", (data) => {
  19. console.log("111", data);
  20. });
  21. this.socket.on("connect_error", (error) => {
  22. console.log("error", error);
  23. });
  24. // setTimeout(() => {
  25. // console.log("this.socket", this.socket);
  26. // }, 3000);
  27. }
  28. async init(callback) {
  29. this.startSocket();
  30. console.log("init-trtc");
  31. const config = {
  32. sdkAppId: 1400653314,
  33. // roomId: "8888",
  34. userId: "123",
  35. };
  36. const { sdkAppId, roomId, userId } = config;
  37. this.socket.on("getSign", async (data) => {
  38. const userSig = data;
  39. this.userSig = userSig;
  40. try {
  41. const client = TRTC.createClient({
  42. mode: "rtc",
  43. sdkAppId: sdkAppId,
  44. userId: userId,
  45. userSig,
  46. });
  47. this.client = client;
  48. // await client.join();
  49. callback();
  50. } catch (error) {
  51. console.log("joinRoom error", error);
  52. callback(error);
  53. }
  54. });
  55. }
  56. }