Communication between a server and unity app - c#

I'm developing an android game using unity. I have a pi-server (static ip, port forwarding, apache 2, php, mysql and phpmyadmin all set up) and I want to use it to store scores from my app to it as well as retrieve top scores for a leader-board. What code should I use to facilitate the kind of communication I want? To be more precise how would I send through the sql code to the server and read the data?
It is the first time I'm attempting to do this kind of thing, so sorry if it is something obvious. Other info: port 80 is set for forwarding, the database is called "game_scores", table is called "highscore" and the table consists of 4 columns: id, username, score and date.

Although it's possible to have your app communicate directly with the MySQL database on your server (through port 3306, not port 80), the standard way to facilitate this kind of connection is to instead implement a web service (REST API) that runs on your server, listens for HTTP requests from your Unity app, and connects to the database to respond to your app with the data it needs. Communicating with your server through HTTP (or similar protocols) creates an abstraction layer that allows you to change or swap out your server or database implementation in the future without changing the client. Here's a decent-looking tutorial that describes how to create a simple REST API with PHP and MySQL. Once you create the REST API on your server, you can send requests to it from Unity using UnityWebRequest.

Related

Realtime updates between C# desktop app, mobile app and SQL server

Can anyone explain the best practice communication flow for a multi-app solution I am currently working on. I will have a MS SQL server database running. This database will be accessed and updated by one or more windows desktop applications and mobile devices(android, iOS and Windows). Whenever any changes are made to the database, the clients should be updated in real time to reflect. The desktop applications are most important and need to be updated with as little delay as possible. The mobile apps on the other hand can tolerate a delay of a minute or so.
My idea is connect the desktop application to the server using signalR, the database will then notify any connected clients of the update via SQLDependency. For the mobile side, I was planning for the server to send notifications to the mobile devices, where upon receipt of the notification, the mobile device would requery the SQL Server (via web service) to update itself.
I guess my question is, is this the best (standard) way to go about this or is there a better/quicker/more robust way to achieve this.
Any advice would be appreciated.
I would suggest you create an API in C# and host in on IIS. Your desktop app (be it web or native) will then use RESTful calls to the APIs.
The API's, hosted on IIS would communicate to SQL Server. For database operations, you would have CRUD (Create, Read, Update, Delete). You would expose a GUID (generated using NEWID() in sql server) to each CRUD method and supply serialized data. E.g. perhaps XML or JSON POSTed to the API.
In my view (and practice), this is the Best option. It is truly global, and the API's can "talk" to any client app capable of making HTTP requests.
So the system becomes:
SQL Server > API/IIS > Client (Android, iOS, JS, Java, Windows, Unix, Mac, etc)
Benefits here also are:
SSL out of the box
RESTful transactions - can use oAuth or write your own (proprietary is harder to hack!)
Highly scalable - IIS support 100's of transactions per second
Do not need to open port 1433 (SQL port) to web (which will open you to hacking). API uses port 443 (HTTPS)
If you write your API using C#, you have many serialze/deserialize functions out of the box.
Hope this answers your question.

Web Interface for a server application

I'm building a server-client application that uses TCP/IP sockets to communicate and i want to build a web interface to manage things.
Technologies:
-TCP/IP Sockets;
-Entity Framework 6 Code First;
My solution consists of four projects:
1) Server (Console application, runs 24/7, recieves data and writes to the db);
2) Client (Console application, runs 24/7 sends data);
3) NetLib (Class Library);
4) Interface (WebForms, entered occasionally, shows data from the db);
As for reading and managing data I'm good because all of them are written to the DB by Server and read by the Interface. But I have an issue: I want to be able to execute actions from the interface such as "Send new config to the client". How should i approach this? Creating a new socket in the interface project and sending it to the client sounds really bad.
Desired solution would be that the Interface somehow "tells" the server to perform such action. How to achieve that?
When your client sends data to the server, have your server send a response (if you are not doing this already). The response can contain any commands that the client needs to perform (such as update client configuration).
Now, your WebForms program needs to inform the server of whatever actions it needs clients to perform. This can be done in many ways, depending on how your system is set up.
The WebForms program can write to the database and have the server read from it.
or
The server can listen on a socket and the WebForms program would connect to this socket. If the WebForms program does not know where the server is, the server could write its address (DNS name/IP address) to the database, and the WebForms program would retrieve this address from DB and connect.
or some other way that suits you

Realtime WebApp: Send the same data for all connected clients at the same time

I want to create simple game which data will be send to clients by the server. Clients should have the same data at the same time, data should be generated by the server. If the server returns some rand() value it could be the same for all. It could generate data via interval 24/h every 1minute new value.
Clients excercise is to guess the value which server return, on succes this player win, and new rand starts.
What technology i should to use, it could be web app availible on browsers. I prefer to create it with c# and websockets, but i dont know how to integrate data for all clients.
Help, thanks.
Luckily Node provides socket.io library which you can use to achieve this level of functionality.
Get Socket.io Details
With socket.io you can broadcast the same data to all connected users using websockets.

Web application client server

I want to create a client/server web application. The client and server can exchange data back and forth. When i say data i mean like a number, for example (0,8,7...), so everytime a client presses a button it sends a number to the server and the server send an acknowledgement back to client. The cleint side i want to put it on the internet so you can access the server from a browser.
Is silverlight socket the way to go? I know theres port restrictions but im planning on using my personal router to open up the ports. Or is socket only for local connections???
Assuming you just have no idea where to start, I'd say you should start by learning about WCF (Windows Communication Foundation). Obviously, start with the beginner's guide. There are some nice introductory videos there that should get you going.
If there is such a thing as Silverlight sockets, you can use them. I'd rather use WCF. Sockets aren't restricted to local connections, but you should be aware that using ports different from 80 in Web applications can restrict some user from accessing your them.
I would like to add that sockets aren't the fastest local connections, but are the base of almost all inter-machine communication.

sending data to the server via port 5555 C#

I am wanting to send some data to a server from a client application to the server via port 5555. I don't have a window's server. My question is if I purchase a .Net Framework hosting service. Would I be able to connect the client application to this server. and could I do it by sending a Post. I am new to this, so excuse me if it doesn't entire make sense. but I am looking for just some little direction. If someone could explain what port 5555 is used for that would be great.
Most hosting provides will only allow incoming connections on the standard web ports (80 for unencrypted traffic/http and 443 for encrypted/https). Outgoing connections are less likely to be restricted, but it is not uncommon that only a few specific ports are open.
If you're developing a custom solution and you do not need a connection that allows you to continually send data back and forth, then a hosted solution will do fine. If you do need the live link or custom port numbers then you'll need to upgrade to a virtual managed server, which is essentially your own virtual copy of a Windows server. This costs more but allows you to listen on custom ports - essentially do as you please.
As for determining the specific use of a port, most networked applications have simply picked one or more numbers without much research. IANA maintains a list of protocols/applications/services and their associated port numbers, but being listed requires that an application is sent and approved.
Custom port for data exchange + tcp/ip has been identified to be very difficult across the Internet, due to firewalls/gateways everywhere.
Why not use REST web service or SOAP web service for data exchange? That has been proved to be a firewall friendly approach as it uses port 80 or 443.

Categories