I have this web application (MVC using C#) that serves like an advertisement in my client's office. My client will open this "advertisement page" and display it on a big screen to their customers.
What happen is, every 30 minutes or so, the page will automatically refresh to fetch latest data from the database, however, they are using WIFI to connect to our server and sometimes the connection is very slow (or lost connection completely). My client requested me to write a code to prevent the page from refreshing if the connectivity is bad or no internet connection. (They do not want to show "No Internet Connection" on their advertisement TV)
I know I cannot do anything from the server side code because it is the client's machine that want to detect the internet connection, so leaving client side code as the only option. I am not good at this, can anyone help me out?
I'd suggest a "ping" sent via ajax:
var timeStart= new Date().getTime();
$.ajax({
url:"url-to-ping-response-file",
success:function(){
var timeNow = new Date().getTime();
var ping = timeNow - timeStart;
//less than one second
if(ping < 1000){
window.location.reload();
}
}
});
You can use the Circuit Breaker Pattern to gracefully handle intermittently connected environments.
Here are 2 open source JavaScript implementations. I have never used either of them, so I cannot attest to their quality.
https://github.com/yammer/circuit-breaker-js
https://github.com/mweagle/circuit-breaker
You can also make use of
if (navigator.onLine) {
location.reload();
}
This will not detect slow internet. Now, I don't understand your web layout but for sites that I work on I tend to get HTML content and DATA as separate calls. I do this with a MVVM/MVC pattern which is worth learning. I use angularjs it is very awesome.
Now.. you can also use good old jQuery to replace the content have a read of this Replace HTML page with contents retrieved via AJAX you could couple this with the .onLine check.
http://www.w3schools.com/jsref/prop_nav_online.asp
Related
In my Unity3D game the Player plays himself through short levels, which at the beginning take about 4 seconds. The goal is to get the best possible clear time in each level. I am currently saving these clear times locally, but I would like to upload them to an SQL Server, to be able to create a leaderboard for each level.
Since performing the SqlConnection.Open() command takes about 1-2 seconds, sometimes also 3, I was wondering whether I should keep a static connection always open, ready to execute any queries that I want to run.
Are there any unwanted and dangerous side-effects when doing it?
Edit: This is the code that I use for opening the SqlConnection.
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder()
{
DataSource = dataServer,
UserID = userId,
Password = password
};
SqlConnection connection = new SqlConnection(builder.ToString());
connection.Open();
First I'll answer this question:
Are there any unwanted and dangerous side-effects when doing it?
Assuming you keep this code in your Game (client) and the SQL Server is not client-side but in a server of yours located somewhere, a simple Client Reverse Engineer will be able to grab your credentials from your connection string and use them to gain Unauthorized access to your database. (NEVER trust the client)
With that being said, I would suggest you use a simple server-side technology (php,Socket App, Java servlet, etc..) along with the SQL that the client will send the information there and then to the database.
Example:
1) Client-> Posts Data -> PHP
2) PHP -> Connects -> SQL Database
3) PHP -> Inserts data -> SQL Database
Like this you can also retrieve the results of your ladder from the database by asking php (or whatever technology you are using) to send your data to the client.
Best wishes in your progress, and feel free to contact me for any additional information!
I want to display progress on my screen/page/view just as happens in console. So when i click install button, my "textarea" control should start displaying progress like
connecting to database
connection successful
running script a.sql
running script b.sql
operation complete
connection closed
Is textarea the correct control for this purpose?
it Seems like there will be too many trips from server to the client just to write the progress on screen how can i minimize it?
To be able to do this easily, you can use SignalR which simplify all the hassle of which underlying technology to be used based on the version of the browser it will choose the best communication protocol (WebSocket, LongPolling..etc).
Behind SignalR, one of the underlying used technologies is websockets, it doesn't send anything except keep an open full duplex channels between server and client, in case any update in the server it will push this update to the client. most popular sites use websockets for keeping open channels between server and client.
SignalR uses actually websockets however it will downgrade to use long polling for example in case of old browsers that don't support websocket's connection upgrades.
You have the option to use the websockets directly in case you are assured your clients use new browsers.
One last thing Stackoverflow as a big and heavy loaded site uses websockets to update once there is a new answer or comment for example.
The general approach for this is just sending an ajax request every second or so and asking for an update from the server.
<div>
<pre id="status">
</pre>
</div>
setInterval(function () {
$.ajax("/getUpdate?someParam=1234").then(function (result) {
// result is whatever JSON object you send from the server
$("#status").innerText = result.someProp;
});
}, 1000);
It might look something like the above. Note that it's doing a request every second and storing the message from the server in the pre element.
Here is the setup:
There are 2 Servers : one at the client site and one on another server
The connection between the two of them is not always on.
I have to have code that replicates the functionality from the Server UI: FileSystem > Status > "Sync Now"
I have to be able to watch the process to ensure that it completes without conflicts before moving I move on the next step.
Can anyone point me to the proper classes in the Raven Client Library to do this? Examples would be greatly appreciated.
You are looking for:
DestinationSyncResult[] syncResults = await store.AsyncFilesCommands.Synchronization.SynchronizeAsync();
This will force your server to push all changes to destinations and return all the details about processed files and errors if happened any. Investigate also more methods exposed by IAsyncFilesSynchronizationCommands:
store.AsyncFilesCommands.Synchronization.XXXXX
You can also use the Changes API mechanism to be notified about server activity. It works the same way like for RavenDB databases. For example:
store.Changes().Where(x => x.Direction == SynchronizationDirection.Outgoing).ForSynchronization().Subscribe(x => { });
I have to built a Client/Server application where the server side is just an application that passes data from one client to the other.
The client will be written in C# and I want to write the server in NodeJS, if that's possible. I'll try to explain the idea behind this.
It's best to explain this if you see this application as a 1vs1 game. So multiple clients can connect to an Async server. One client will be matched with another client as in a Player1 / Player2 context.
Player1 can make a "move". A POCO will be converted to a JSON format for example. That will then be send to the server and the server should know to what opponent (Player2) it should sent the data. Then in return Player2 can make a move and then new data will be send back through the server back to Player1.
So the server needs to have a list of all the connected players/clients. So it can keep track of what player play vs who and what scores they have etc. Basically, a stateless environment.
I've been thinking about writting the server in C# with the SocketAsyncEventArgs class. But since NodeJS/Socket.IO is already an optimized lib, that would save time if I could do it in that.
I just need to know if it's possible to do this in NodeJS. Or should I stick to a C# server?
Okey,
I argue that it is possible if you use something like SocketIO4Net.Client at your C# side to open connection to your node.js server.
From the docs:
SocketIO4Net.Client can interact with Socket.IO instances in a very
similar manner as Socket.IO JavaScript clients. This project aims to
fill the JavaScript client and C# client gap for Socket.IO, and retain
a very similar application experience.
And for the purpose of storing player lists etc. at server, it is easy. You define javascript object just as players where you add your connected clients and use for example socket.id as identifier.
// Players currently at server
var players = {};
you do this usually by listening socket connection event, for example:
... somewhere in your code ...
var io = require('socket.io').listen(httpServer);
... somewhere in your code ...
// run on new client connection..
io.sockets.on("connection", function (socket) {
// adding to list of players you have in your server
// Player can be your own declared class as you like it to be
players[socket.id] = new Player({
nickname: "playerNameExample"
});
});
However, if your game is more like match-making 1vs1 it would be worth to consider that you add always your separate playing partners to different rooms in server. So exchanging messages travels only between relevant participants. See socket.io rooms
As for the resources where to go next: best choice is to just google "multiplayer game with socket.io" which will give wide variety of results. Just as this: Building-multiplayer-games-with-nodejs if you are completely new to node.js googling hello world with node.js might be better to do first.
Due note: If you don't know the basics and haven't tried programming anything with node.js you are not ready to make working multiplayer game, advance step by step.! ( and yes I am looking this now from game perspective as you instructed but behavior of games can be applied to many apps as well ). Cheers.
I must build a Application that will use Webclient multiple times to retrieve every "t" seconds information from a server.
Here is a small plan to show you what I'm doing in my application:
Connect to the Web Client "USER_LOGIN" that returns me a GUID(user unique ID). I save it and keep it to use it in future Web Client calls.
Connect to the Web Client "USER_GETINFO" using the GUID I saved before as parameter. This Web Service returns an array of strings holding all my personal user information( my Name, Age, Email, etc...). => I save the array information this way: Textblock.Text = e.Result[2].
Starting a Dispatcher.Timer with a 2 seconds Tick to start my Loop. (Purpose of this is to retrieve information and update it every 2 seconds)
Connect to the Web Client "USER GETFRIEND", wich is in my Timer, giving him the GUID as parameter. It returns me an array filled with my friends informations(Name, email, message, etc...). I inserted this WebClient in the timer so my friend list refreshes every 2 seconds.
I am able to create all the steps without any error until step 3. When I call the "USER_GETFRIEND" Web Client I am facing two major problems:
On one side I noticed that my number of Thread increased dramatically. => I always thought that when a WebClient had finished its instructions it would shut down by itself, but apparently that does not happen in Asyncronous calls.
And on the other side I was surprised to see that using the same proxy for two Webclient calls(ie: if i declare test.MainSoapClient proxy = new test.MainSoapClient()), the data i would retrieve from "USER_GETFRIEND" e.Result, was sent directly to my "USER_GETINFO" array. And so my Name and Email adresses on the UI were replaced by the same value in the USER_GETFRIEND array. So my Name is changed to my friends email and so on...
I would like to know if it's possible to close a WebClient call(or Thread) that I am not using anymore to prevent any conflicts? Or if someone has any suggestion concerning my code and the way i should develop my application please feel free to propose.
I got the answer a few weeks ago and figured out it was important to answer my own question.
My whole problem was that I wasn't unsubscribing from my asynchronous calls and that I was using the same proxy class from "Add Service reference":
So when I was using:
proxy.webservice += new Eventhandler<whateverinhere>(my_method);
I never did:
proxy.webservice -= new Eventhandler<whateverinhere>(my_method);
Hope it will help someone.