Unity5 Beta : WebGL + Unity WebSockets plug-in

Hi,

Quote : http://blogs.unity3d.com/2014/04/29/on-the-future-of-web-publishing-in-unity/
"WebGL in Unity 5.0
We plan to WebGL support available in Unity 5.0 as an early-access add-on. Early-Access means that it will be capable of publishing content to WebGL (like the examples above), but it will have some limitations in features and in browser compatibility. In particular, the following features will not be supported:

  • Networking other then WWW class (a WebSockets plug-in is available)"

I’m searching for websockets plugin in unity5.0.9beta. Could unity shed some lights on the unity websockets plugin availability in unity5 ? thank you!

Br,
Peter

2 Likes

Bump

You can currently get it here (we still need to find a permanent home for these):

http://files.unity3d.com/jonas/WebSockets.unitypackage

FYI, some third party products already added support for this. Check out exitgames’ Photon, and SmartFoxServer.

3 Likes

There’s also an example of using Socket.IO with WebGL:

1 Like

Thanks jonas!

My project is recreating an MMO via Unity WebGL (for the client). We already have existing server-side code thats been around for 12 years.

This is our implementation for exposing the Server to Websockets so we can communicate with the client: http://wiki.eqemulator.org/p?Project_Web_Sockets

This is the complete serverside code: GitHub - EQEmu/Server at web_interface

1 Like

I did not look into your implementation, but just in case people are interested: Websockify is a great tool when you need websockets client to connect to existing IP services (by implementing a bridge from WebSockets ↔ IP). We use the same for the profiler connection to the unity editor in WebGL.

https://github.com/kanaka/websockify

2 Likes

hey Jonas, let’s say I want to add a protocol to the end of the Host url in EchoTest.cs. How would I go about this?

The Build:
http://files.unity3d.com/jonas/WebSockets.unitypackage

The example -
Host: 38.103.160.13:9080
Sec-WebSocket-Protocol: eqemu

Thus making line 9 in EchoTest.cs look something like this:
WebSocket w = new WebSocket(new Uri(“ws://38.103.160.13:9080”, “eqemu”));

Look at how the code passes the url to JavaScript in the constructor. You should be able to do the same for a protocol.

1 Like

I assume you are talking about line 121 in particular (also lines 13-17) of WebSocket.cs in the WebSockets.unitypackage i linked above.

http://hastebin.com/wojubibigu.coffee

Could use some additional assistance if anyone with time could chime in.

Also, fun fact, this thread is in the top 25 of the total 849 threads in terms of viewer popularity in the Unity 5 pre-order sub forum.

ya, I’ve been talking briefly with Photon’s Tobias about their early access WebGL support and its a slow process, don’t think they have much support right now either.

Figured out my problem. This is how you add a protocol to the WebSocket package. Might be helpful for someone in the future.

line 121:
http://hastebin.com/yesuvazoyi.coffee

change:
m_Socket = new WebSocketSharp.WebSocket(mUrl.ToString());

to:
m_Socket = new WebSocketSharp.WebSocket(mUrl.ToString(),“eqemu”);

My result:

There is also a Unity3d SocketIO library from NetEase folks: GitHub - NetEase/UnitySocketIO: socket.io client for unity3d., may be worth a try.

1 Like

Did someone manage to get plugin working for WebGL build?
It works fine in Editor(EchoTest), but if I build for WebGL I get:

Unity 5 b21

Any idea a library for unity 5 WebGL + NodeJS + socket.io?

Well I use directly socket.io from html javascript and works fine.

1 Like

Hi! I get the same error as ortin above when testing the Echotest WebGL build in localhost (Unity 5 b20) - any help would be much appreciated:

Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
WebSocket is already in CLOSING or CLOSED state.
Error: Abnormal disconnection.

thanks

To fix edit Assets\Plugins\WebSocket.jslib and in line #9 replace

socket: new WebSocket(str, ['soap', 'xmpp']),

with

socket: new WebSocket(str),

I found another socket io C# implementation: GitHub - Quobject/SocketIoClientDotNet: Socket.IO Client Library for .Net which is a direct port from the javascript client and supports SocketIO 1.0.

But then as @Osterion mentioned, for WebGL builds it could be just easier to use the javascript client right? Is there any value in using C# and then converting it back into JS?

I don’t think you cam use socket connections from within unity webgl. So by JavaScript, I think you need to use actual JavaScript on your host page and proxy between webgl and the JavaScript. So c# would be out as well, at least as far as directly utilizing websockets.