淘先锋技术网

首页 1 2 3 4 5 6 7

1.进入如下已打包的UE4项目路径:

E:\WindowsNoEditor\Engine\Source\Programs\PixelStreaming\WebServers\SignallingWebServer

2.打开文件:cirrus

3.在第376行后加入如下代码:

_RemoveExtmapAllowMixed(msg);// 替换sdp协议内新内容

此块整体代码如下:

playerServer.on('connection', function (ws, req) {// Reject connection if streamer is not connectedif (!streamer || streamer.readyState != 1 /* OPEN */) {ws.close(1013 /* Try again later */, 'Streamer is not connected');return;}let playerId = ++nextPlayerId;console.log(`player ${playerId} (${req.connection.remoteAddress}) connected`);players.set(playerId, { ws: ws, id: playerId });function sendPlayersCount() {let playerCountMsg = JSON.stringify({ type: 'playerCount', count: players.size });for (let p of players.values()) {p.ws.send(playerCountMsg);}}ws.on('message', function (msg) {console.logColor(logging.Blue, `<- player ${playerId}: ${msg}`);try {msg = JSON.parse(msg);} catch (err) {console.error(`Cannot parse player ${playerId} message: ${err}`);ws.close(1008, 'Cannot parse');return;}if (msg.type == 'offer') {console.log(`<- player ${playerId}: offer`);_RemoveExtmapAllowMixed(msg);// 替换sdp协议内新内容msg.playerId = playerId;streamer.send(JSON.stringify(msg));} else if (msg.type == 'iceCandidate') {console.log(`<- player ${playerId}: iceCandidate`);msg.playerId = playerId;streamer.send(JSON.stringify(msg));} else if (msg.type == 'stats') {console.log(`<- player ${playerId}: stats\n${msg.data}`);} else if (msg.type == 'kick') {let playersCopy = new Map(players);for (let p of playersCopy.values()) {if (p.id != playerId) {console.log(`kicking player ${p.id}`)p.ws.close(4000, 'kicked');}}} else {console.error(`<- player ${playerId}: unsupported message type: ${msg.type}`);ws.close(1008, 'Unsupported message type');return;}});function onPlayerDisconnected() {players.delete(playerId);streamer.send(JSON.stringify({type: 'playerDisconnected', playerId: playerId}));sendPlayerDisconnectedToFrontend();sendPlayerDisconnectedToMatchmaker();sendPlayersCount();}ws.on('close', function(code, reason) {console.logColor(logging.Yellow, `player ${playerId} connection closed: ${code} - ${reason}`);onPlayerDisconnected();});ws.on('error', function(error) {console.error(`player ${playerId} connection error: ${error}`);ws.close(1006 /* abnormal closure */, error);onPlayerDisconnected();});sendPlayerConnectedToFrontend();sendPlayerConnectedToMatchmaker();ws.send(JSON.stringify(clientConfig));sendPlayersCount();
});

4.在文件末尾追加如下代码

/*** sdp协议更新的方法* @param desc offer内容* @returns {string}*/
function _RemoveExtmapAllowMixed(desc) {if (desc.sdp.indexOf('\na=extmap-allow-mixed') !== -1) {const sdp = desc.sdp.split('\n').filter((line) => {return line.trim() !== 'a=extmap-allow-mixed';}).join('\n');desc.sdp = sdp;return sdp;}
}