I have a TCP server (which I can not change/modify) running which sends data and I want it to connect to Unity WebGL build.So first of all I know it is natively not supported to connect to a TCP server in WebGL. I searched for workarounds and tried the following : Use Websockify to enable connection from the TCP server to a WebSocket. I used the NativeWebsocket Library but it would not connect to the server unfortunately. I also read about these blogs :
How to let a Unity WebGL build connect to a local python server?
https://forum.unity.com/threads/webgl-tcp-client.738419/
But they didn't really help either. Has anyone tried this or knows how to make it work (if it is even possible :) )?
Thanks in advance
Unity3D uses the .NET C# libraries, so even though Unity doesn't explicitly provide a TCP library, you can still use .NET's TCP APIs. It's worth noting that not all platforms will support this, for example, WebGL doesn't support TCP communication.
See the example in .NET documentation for help on implementation: System.Net.Sockets TcpClient
For WebGL, it is impossible to use TCP communication, so you'll have to wrap the server in some kind of HTTP/Websockets transport - you cannot leave the server as is, it will not work because the protocol cannot be communicated with for security reasons.
If you have any question as to the specifics of implementation, feel free to ask in the comments - I don't have enough information to elaborate further on your specific application
Related
I try to connect between NetworkManager (HLAPI) in Unity3D and Socket server.
I have already tried to connect two Unity applications using HLAPI or LLAPI. It works well. But HLAPI or LLAPI Unity client can't connect to Socket server (python).
I think that HLAPI or LLAPI supports to connect among Unity3D applications. Is it right?
Is it possible to connect between NetworkManager (HLAPI) in Unity3D
and socket server?
The short answer is No.
These are two different protocols.
I once tried it. It seems to connect secretly then immediately disconnect. I can't remember if I tried it with TCP or UDP but you can't just do that.
HLAPI or LLAPI are built on on top of the UDP protocol and possibly TCP too in newer Unity version. In order to connect to it with an application not made with Unity, you will have to reverse engineer the HLAPI or LLAPI protocol and understand how they both connect to each other(Handshake) then build a custom API for python using raw socket.
If you want to communicate between Unity and a program made with python, I suggest you use standard socket (TCP/UPD). This is more less hassle and will allow your app to work with any language that supports socket.
I have a game I am working on and I heard that UDP is superior for real-time games. I know that socket.io uses TCP and was wondering if there is some way to switch it to UDP. I tried looking it up but only found posts from around 2012 which said UDP is only experimental in browsers.
From a standard browser, it is not possible.
From a browser client, socket.io uses either the http or the webSocket transport. Both http and webSocket are TCP connections, not UDP connections. So the browser client socket.io does not use UDP - it uses TCP.
As best I know, there is no standard UDP support in browsers accessible from regular HTML page Javascript so you can't even really try to build your own layer that uses UDP.
Other references on the topic:
Why Can't I Send UDP Packets From a Browser
Reading from udp port in browser
Chrome Supports TCP and UDP Sockets
Write a chrome extension to support UDP in browser
How to send a UDP Packet with Web RTC - Javascript?
How to talk to UDP sockets with HTML5?
Reading from udp port in browser
UDP can be a desirable transport for some circumstances when you want the packet to be delivered as soon as possible, but if it can't be delivered immediately, then just drop it. This is sometimes useful in gaming or even video streaming where the next packet will just contain the next update so if the previous one didn't come through, then no big deal and you'd rather not have TCP try to retransmit the lost packet. But, browsers don't support using the UDP protocol from web page Javascript.
If you want to connect to a UDP device or server from a browser, you will have to use some sort of proxy so your browser code can talk to the proxy over TCP (either http or webSocket) and then the proxy can handle the actual UDP communication with the device.
It would be possible to use the socket.io library from node.js or some other non-browser platform and write your own UDP transport add-in for socket.io that is built on the native UDP support in your platform. I believe socket.io has a somewhat pluggable transport so you could try to make your own transport and then configure both client and server to use that transport. This is not possible from the browser (without a native code plug-in installed in the browser) because there's no underlying UDP support in the browser that you could build your transport on, but in non-browser environments like node.js, you could do that.
It might be a good idea to use webRTC in this case which is UDP in nature.
Although the question is already answered, I want to point out that there are ways to implement socket.io with UDP. For example dgram does exactly what you are looking for.
This is a tutorial for socket.io + UDP with dgram.
UPDATE:
Alexandre Lacheze developed a node.js package that brings UDP to browser. It also supports socket.io. So the answer is somehow obsolete now.
UPDATE 2:
It turn out it is just a simulated UDP. Not an actual UDP protocol running on browser.
I want to connect to a locally running TCP service from a web application I'm building using the Play framework and scala.
I'm not sure how to open this connection and send commands to it, should I be writing raw socket code? Also, how should I be managing the connection? Can I just open the connection once and send commands from each web request to it? What if the connection is closed or falls over?
Not sure Pay will be of much help here, most of its modules focus on HTTP communication. You should have a look at Akka-IO though.
Is there an easy way to open a TCP socket to connect a TCP server? Should I use socketjs, jsocket or something else?
I try to develop a cross-platform application to connect to a TCP server and pull data from it.
Thanks
No, we do not have an API for that at present. But you can use WebSockets on iOS, and libraries like socket.io which fall back to long-polling on Android.
If you email us at support#trigger.io we can advise on your specific requirements and see if it could make sense to offer a native API.
I'm the author of trigger.io-tcp, an android-only trigger.io native module that allows you to open TCP sockets for sending/receiving data.
You can use it if you need sockets only for Android or fork it and write the iOS part.
I am working with the BlueZ libraries for managing the bluetooth stack under linux. I am trying to open a socket that should connect with a specific service whose UUID is known. I have successfully tried to open sockets between a server and a client following the examples here:
http://people.csail.mit.edu/albert/bluez-intro/c404.html
where it is also described how to explore services in a remote host; however, what I cannot figure out is how to specify the UUID while connecting the socket. This is something quite straightforward on other contexts, as it happens when using Android libraries; however, with BlueZ I haven't found examples on the web.
Thanks for the help!
Stefano
-- added some details in a reply... don't know if it's right or not; if not, sorry
thanks for your help!
In my post I said I wanted to connect a socket with a specified UUID since I had in mind a function like createRfcommSocketToServiceRecord as can be found in:
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createRfcommSocketToServiceRecord%28java.util.UUID%29
which returns a socket given a certain UUID. I am developing two softwares, one being the client and the other the server, therefore it's a service I am creating, not a standard one. I publish such service on the server with a specified UUID, and I would like to connect to it from the client. The server is running fine, since with an android platform, exploiting the mentioned createRfcommSocketToServiceRecord method, I am able to connect to the server with the right service.
The example you posted is very clear as well, but unfortunately it is in Python, while (I forgot to point out) I am using Bluez as a C library. I am able to exploit the SDP and inspect services on a remote server; however, when it is the time of setting up the socket, I don't see how to specify the port. I thought about the rc_channel, but as far as I understood it is the bluetooth channel (ranging from 1 to 11 or something similar). Could you please point out Where I should be able to specify the port while creating the socket in the client? And where am I able to find the right port in the complex structure the sdp is dealing with? -- referred to:
http://people.csail.mit.edu/albert/bluez-intro/x604.html
Thanks again for the help!
You don't connect sockets by UUID. You use a socket to connect to remote device and browse its SDP to discover the UUIDs it offers, and map that to a socket port. Here's an example of this process using PyBluez, python wrappers above BlueZ
http://people.csail.mit.edu/albert/bluez-intro/x290.html