I am having a strange issue with a WebGL build, where all the incoming network messages to the browser build cannot be received while I am moving my mouse around the browser window. I am using some very simple javascript code to recieve messages from a WebRTC data channel:
This is the relevant bit of javascript:
mergeInto(LibraryManager.library, {
initWebRTC: function () {
this.recMsgQueue = [];
this.webrtcConnection = new RTCPeerConnection();
this.dataChannel = webrtcConnection.createDataChannel("datachannel", {
ordered: false,
maxRetransmits: 0
});
dataChannel.onmessage = function (msg) {
this.recMsgQueue.push(new Uint8Array(msg.data));
}
.bind(this);
},
recWebRTCMsg: function(buf, buf_len)
{
if(this.recMsgQueue.length > 0)
{
var rec_data = this.recMsgQueue.shift();
if(rec_data.length > buf_len)
return 0;
HEAPU8.set(rec_data, buf);
return rec_data.length;
}
return 0;
}
}
And the relevant code in C# to rec messages:
[DllImport("__Internal")]
private static extern void initWebRTC();
[DllImport("__Internal")]
private static extern int recWebRTCMsg(byte[] buf, int buf_len);
public int rec(ref byte[] msg_buf)
{
int ret_bytes = recWebRTCMsg(msg_buf, msg_buf.Length);
return ret_bytes;
}
and in another class i just call rec inside FixedUpdate
protected virtual void FixedUpdate()
{
//call rec and handle message
}
Some more information:
- This does NOT happen while the Chrome developer console is open.
- This does NOT happen while the cursor is stationary. The cursor must be over the correct window and MOVING. The window also does not need to be selected for this to happen.
- Opening two instances of the game in different Chrome windows, only the one being moused over has this issue.
- This problem is only known to exist in Chrome. Firefox is also tested and working without the problem.
- In the Unity editor and Windows builds there are no issues (in these cases I am using a simple UDP connection and obviously not using a browser)
- The architecture is such that the server is authoritative, so the browser just appears to lag out, as if the connection has been dropped (but physics and everything still continue).
- Packets from the browser (like an input update from the player) are still being sent and received by the game server. The server is behaving as if nothing is wrong, updating the player movement/position as soon as input is given in the browser. So, it seems that the problem is only in one direction.
As a simple test, I purposely caused this to happen by constantly moving the cursor around the window for a while. The result is that, when I stop moving the cursor, I can see a few thousand DTLS packets suddenly appear at once in wireshark, and the browser instantly catches up to the server state. From all I can gather, the browser window that is currently running the game will simply refuse to rec WebRTC packets while I’m moving my cursor over it. This doesn’t make any sense to me.
I think that the next step for me is to try and get the WebRTC connection working with Firefox, so I can at least understand whether it’s an issue with Chrome or Unity.