mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
commit
ceb439e594
4 changed files with 36 additions and 27 deletions
|
|
@ -1,13 +1,11 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/giongto35/cloud-game/config"
|
||||
"github.com/giongto35/cloud-game/cws"
|
||||
"github.com/gorilla/websocket"
|
||||
pionRTC "github.com/pion/webrtc"
|
||||
)
|
||||
|
||||
type BrowserClient struct {
|
||||
|
|
@ -16,15 +14,23 @@ type BrowserClient struct {
|
|||
|
||||
// RouteBrowser are all routes server received from browser
|
||||
func (s *Session) RouteBrowser() {
|
||||
iceCandidates := [][]byte{}
|
||||
|
||||
browserClient := s.BrowserClient
|
||||
|
||||
browserClient.Receive("heartbeat", func(resp cws.WSPacket) cws.WSPacket {
|
||||
return resp
|
||||
})
|
||||
|
||||
browserClient.Receive("icecandidate", func(resp cws.WSPacket) cws.WSPacket {
|
||||
log.Println("Received candidates ", resp.Data)
|
||||
iceCandidates = append(iceCandidates, []byte(resp.Data))
|
||||
return cws.EmptyPacket
|
||||
})
|
||||
|
||||
browserClient.Receive("initwebrtc", func(resp cws.WSPacket) cws.WSPacket {
|
||||
log.Println("Received user SDP")
|
||||
localSession, err := s.peerconnection.StartClient(resp.Data, config.Width, config.Height)
|
||||
localSession, err := s.peerconnection.StartClient(resp.Data, iceCandidates, config.Width, config.Height)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
log.Println("Error: Cannot create new webrtc session", err)
|
||||
|
|
@ -134,21 +140,6 @@ func (s *Session) RouteBrowser() {
|
|||
|
||||
return req
|
||||
})
|
||||
|
||||
browserClient.Receive("candidate", func(resp cws.WSPacket) (req cws.WSPacket) {
|
||||
// Unuse code
|
||||
hi := pionRTC.ICECandidateInit{}
|
||||
err := json.Unmarshal([]byte(resp.Data), &hi)
|
||||
if err != nil {
|
||||
log.Println("[!] Cannot parse candidate: ", err)
|
||||
} else {
|
||||
// webRTC.AddCandidate(hi)
|
||||
}
|
||||
req.ID = "candidate"
|
||||
|
||||
return req
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// NewOverlordClient returns a client connecting to browser. This connection exchanges information between clients and server
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ func (s *Session) RouteOverlord() {
|
|||
log.Println("Start peerconnection from the sdp")
|
||||
peerconnection := webrtc.NewWebRTC()
|
||||
// init new peerconnection from sessionID
|
||||
localSession, err := peerconnection.StartClient(resp.Data, config.Width, config.Height)
|
||||
localSession, err := peerconnection.StartClient(resp.Data, nil, config.Width, config.Height)
|
||||
oclient.peerconnections[resp.SessionID] = peerconnection
|
||||
|
||||
if err != nil {
|
||||
|
|
|
|||
4
static/js/ws.js
vendored
4
static/js/ws.js
vendored
|
|
@ -162,7 +162,6 @@ function startWebRTC() {
|
|||
log(`iceConnectionState: ${pc.iceConnectionState}`);
|
||||
|
||||
if (pc.iceConnectionState === "connected") {
|
||||
|
||||
//conn.send(JSON.stringify({"id": "start", "data": ""}));
|
||||
}
|
||||
else if (pc.iceConnectionState === "disconnected") {
|
||||
|
|
@ -188,6 +187,9 @@ function startWebRTC() {
|
|||
// TODO: Fix curPacketID
|
||||
conn.send(JSON.stringify({"id": "initwebrtc", "data": session, "packet_id": curPacketID}));
|
||||
} else {
|
||||
//pc.addIceCandidate(event.candidate).catch(e => {
|
||||
//log("Failure during addIceCandidate(): " + e.name);});
|
||||
conn.send(JSON.stringify({"id": "icecandidate", "data": JSON.stringify(event.candidate)}));
|
||||
console.log(JSON.stringify(event.candidate));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,13 +92,13 @@ type WebRTC struct {
|
|||
}
|
||||
|
||||
// StartClient start webrtc
|
||||
func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, error) {
|
||||
//defer func() {
|
||||
//if err := recover(); err != nil {
|
||||
//log.Println(err)
|
||||
//w.StopClient()
|
||||
//}
|
||||
//}()
|
||||
func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width, height int) (string, error) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Println(err)
|
||||
w.StopClient()
|
||||
}
|
||||
}()
|
||||
|
||||
// reset client
|
||||
if w.isConnected {
|
||||
|
|
@ -195,11 +195,27 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e
|
|||
return "", err
|
||||
}
|
||||
|
||||
// Parse candidates list
|
||||
for _, bcandidate := range iceCandidates {
|
||||
iceCandidate := webrtc.ICECandidateInit{}
|
||||
if err := json.Unmarshal(bcandidate, &iceCandidate); err != nil {
|
||||
log.Println("Cannot parse ", bcandidate)
|
||||
continue
|
||||
}
|
||||
w.connection.AddICECandidate(iceCandidate)
|
||||
}
|
||||
|
||||
answer, err := w.connection.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = w.connection.SetLocalDescription(answer)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Sendback answer from server
|
||||
localSession := Encode(answer)
|
||||
return localSession, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue