- Home /
Android client to C# server communication, how to?,How to have an Android app communicate with web server
Hello everyone. I'm working on a small project (just a proof of concept really) where I have an Andorid app made in Unity, which should receive inputs (binary data) from a server running on my PC and possibily send back some data too. Nothing too heavy in both cases. I want good performance and possibly ease of use for me during the implementation. I'm not really an expert when it comes to networking, so I'm trying to figure out the best way to do this. I seems to have only two ways really.
1) Communicate over HTTP, use simple GET/POST requests. I already have a simple HTTP server that I implemented in C# running (haven't done anything Unity/client side). I know of UnityWebRequest, which I think should do everything I want pretty easily. My worry is performance, I know HTTP is sort of heavy, with an overhead that I have to account for 60+ times per second.
2) Use WebSockets. These look cool for perfomance and not too complicated, C# has them already implmentend in .NET. But Unity does not. I checked various plugins for this and they're either deprecated or not working. Unity only seems to implement them for WebGL projects (which is useless to me because nothing goes through a browser). Is there anyway I can use the Unity implementation outside of WebGL projects? Or maybe can I just use the standard C# .NET implementation with Unity (and how, if so)?
So, should I prefer WebSockets if there is a way to make them work in Unity? Or is HTTP fine for a simpler project?
Answer by toddisarockstar · Oct 24, 2018 at 02:06 AM
you can use the .net library in unity simply by putting this at the very top of your script:
using System;
using System.Net;
using System.Net.Sockets;
I have been using TcpClient for my andriod games and it works great to communicate basic data to a TCP server.
if you want to use http, Unity also has its own functions that work well to to grab http requests. here is the most simple:
Answer by Benjamin87 · Oct 24, 2018 at 02:58 AM
Tcp socket or http, it depends on what kind of app you want to build. To be more precisely, it depends on how often your C/S talk with each other.
For instance, your app is kind of a competitive game based on lockstep, which may be C/S exchange messages 20-50 times per second. It will make you suffer from massive channel creations / deletions in each message send over HTTP. In this situation, it will be a better choice to build over socket.
On the contrary, your app, for example, is a newspaper/magazine for users to read. The communication of C/S happens every few seconds. After fetching raw data from server, there is few C/S communication afterwards. Http is good enough here.
Http(WWW) client is a built-in module in Unity. If you want to use socket, you should use C# Socket class under namespace System.Net.Sockets.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Unity server connection time out 0 Answers
ontriggerenter will not work with !islocalplayyer 0 Answers
Dedicated server lobby problem 0 Answers