index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // wrt + socket + contrl
  2. const io = require("socket.io-client")
  3. const RTCVideoSource = require('./lib/videoSource')
  4. //const PWM = require('./lib/Pwm')
  5. const { RTCPeerConnection, MediaStream } = require("wrtc")
  6. const HOST = 'ws://10.10.3.196:7896'
  7. class CarServer {
  8. constructor(HOST) {
  9. this.webRTC = null
  10. this.mediaStream = null
  11. this.videoSource = null
  12. this.socket = io(HOST, {
  13. auth: {
  14. roomID: "feiCar",
  15. name: 'car'
  16. }
  17. });
  18. this.socket.on('connect', () => {
  19. this.connect()
  20. })
  21. this.socket.on('leaved', user => {
  22. console.log(`${user.name}离开${user.roomID}房间`)
  23. // 用户离开移除RTC
  24. this.destroyed()
  25. })
  26. this.socket.on('joined', user => {
  27. console.log(`${user.name}加入${user.roomID}房间`)
  28. // 发送offer
  29. this.createOffer()
  30. })
  31. this.socket.on('msg', data => {
  32. console.log('用户信息', data.type);
  33. this.msg(data)
  34. })
  35. this.socket.on('connect_error', err => {
  36. console.log('连接错误', err)
  37. this.destroyed()
  38. })
  39. }
  40. // 连接
  41. connect() {
  42. console.log('连接成功,开始初始化webrtc')
  43. // 初始化rtc
  44. this.webRTC = new RTCPeerConnection()
  45. // 初始化媒体源
  46. this.mediaStream = new MediaStream()
  47. this.videoSource = new RTCVideoSource()
  48. // 加入媒体源
  49. const videoTrack = this.videoSource.createTrack()
  50. this.mediaStream.addTrack(videoTrack)
  51. this.webRTC.addTrack(videoTrack, this.mediaStream)
  52. // 开始媒体
  53. this.videoSource.start()
  54. // 监听ice
  55. this.webRTC.onicecandidate = event => {
  56. if (event.candidate) {
  57. // 发送ICE
  58. this.socket.emit('msg', {
  59. type: 'candidate',
  60. candidate: event.candidate
  61. })
  62. }
  63. }
  64. // 监听ice状态
  65. this.webRTC.oniceconnectionstatechange = () => {
  66. if (this.webRTC.iceConnectionState === 'failed' || this.webRTC.iceConnectionState === 'closed' || this.webRTC.iceConnectionState === "disconnected") {
  67. console.log('ICE 连接失败')
  68. this.destroyed()
  69. }
  70. }
  71. }
  72. // 用户信息
  73. msg(data){
  74. if (data.type === 'answer') {
  75. // 设置本地应答
  76. this.webRTC.setRemoteDescription(data.answer)
  77. } else if (data.type === 'candidate') {
  78. // 本地设置ICE
  79. this.webRTC.addIceCandidate(data.candidate)
  80. } else if (data.type === 'conctrl') {
  81. // 控制信息
  82. //PWM.changPWM(data.conctrl)
  83. } else if (data.type === 'startRTC') {
  84. // 向其它房间人发送offer
  85. this.createOffer()
  86. }
  87. }
  88. // 创建offer
  89. async createOffer() {
  90. // 创建offer
  91. const offer = await this.webRTC.createOffer()
  92. this.webRTC.setLocalDescription(offer)
  93. // 发送offer
  94. this.socket.emit('msg', {
  95. type: 'offer',
  96. offer: offer
  97. })
  98. }
  99. destroyed() {
  100. if (this.webRTC) this.webRTC.close()
  101. if (this.videoSource) this.videoSource.stop()
  102. this.webRTC = null
  103. this.mediaStream = null
  104. this.videoSource = null
  105. process.exit(1)
  106. }
  107. }
  108. new CarServer(HOST)