In the latest (4.5.4) version of Unity3D, is it possible to cancel a pending WWW class (HTTP) request and have it close the socket/connection? I've tried myWWWObject.Dispose, at least in the editor play mode, and it isn't working.
This forum post on Unity forums has conflicting answers. One person says it works, then another says it doesn't but the answer they provide makes me think they only care about resuming the coroutine and not closing the actual connection.
I'm writing a game that needs to work on iOS, Android and Facebook Canvas (Web Player), and I'm using long polling to my own server. There are times when I want to cancel an existing poll and post a new one. In some cases (kind of odd behavior but possible) the user could cause that to happen over and over many times in a row. I don't want all of those connections to stay open on my server, or client side, even though it is easy for my code to ignore any result/response.
I can probably work around this with design changes, but would rather not, if there is a robust way to close/cancel/abort/stop the request. I'm pretty sure all of the 3 platforms I need to support have ways to do this natively, but not sure I want to use native plugins instead of WWW class.
I'm not quite sure but I think that when you mean to close the socket connection you are lowering down to the TCP/IP or UDP layer. Then, you are not in control of this layer from a WWW gameObject as far as I know. So technically even if you cancel out the 'transfer', the socket will have to wait for a TCP/IP timeout to be dropped by the OS... I think...
I found this link that might shed some light at your particular issue: http://answers.unity3d.com/questions/676443/wwwdispose-doesnt-work.html
Still I think that .Dispose() might still not close the actual socket like you want. Only in the case that you are directly handling via Thread, TcpListener and TcpClient you are allowed to issue an actual socket close().
Like for example:
TcpListener myTcpListener;
TcpClient myTcpClient;
void OnApplicationQuit()
{
try
{
myTcpClient.Close();
isTrue = false;
}
catch(Exception e)
{
Debug.Log(e.Message);
}
// You must close the tcp listener
try
{
myTcpListener.Stop();
isTrue = false;
}
catch(Exception e)
{
Debug.Log(e.Message);
}
}
Related
I have been working with SignalR package for a while, so now I have a task which requires me to create two websockets connection. One channel is for taking about 50 photos (it comes from server having about 2MB size for each, which is pretty big), the second is for getting time, user's auth, keep user's data, so my question is:
How can I open two websockets connection concurrently?
I have read that the websocket works synchronously and it is impossible to make it asynchronous (I guess?...), so may be, someone have had the same issue and solved it?
Now with SignalR package I need to start my websockets connection every time I want to use request to the server. For example, now it looks like this:
The first request on page
Future<String> getTime()async{
//some code for building url which I took from from SignalR official docs
await connection.start()
hubconnection.invoke('getTime');
}
the second request on this page
Future<String> getUsersData()async{
//some code for building url which I took from from SignalR official docs
await connection.start()
hubconnection.invoke('getUsersData');
}
So I am not sure about this line: await connection.start() because I think this means that every time I go to a page where there can be up to five such requests, the websocket starts every time, which greatly affects the performance of the app. Is it possible to make this line shared between all requests once or is there some way to improve the work?
So after this research I was thinking may be I can open two websockets request to make workload less?
I implemented an UWP Server Socket following the sample here and it correctly works.
Now I want to make the app able to continuously accept requests, but I expect that when the app is suspendeded and a client sends a request, the server is not able to respond. If I am correct, what is the best way to avoid this status change? If possible, I would prefer a solution with Extended Execution instead of implementing a Background Task, but I don't know if the following code in the OnSuspending method is enough to keep the app in the Running status:
var newSession = new ExtendedExecutionSession();
newSession.Reason = ExtendedExecutionReason.Unspecified;
newSession.Revoked += SessionRevoked;
I saw people calling a "LongRunningWork()" function in other samples, but in my case the code to execute is already defined in the code-behind of the view as shown in the link above, so I would like simply keeping the app always running. Keep in mind that it is a LOB application, so I don't have Store limits.
I have an application (Laravel + MongoDB running on Nginx) where I pull some data from the database and render it on the screen. The application focusses on multiple real life objects. Once an object is turned on (is_on equals to true in the database), the timer on the screen needs to start ticking. Once the object is turned off (is_on equals to false in the database) the clock stops ticking and resets to 0. The format of the clock is HH:MM:SS. So it shows how long the real life object is turned on.
My problem is that I don't really now how to save/implement such timer. When the user request the page, I pull the necessary data from the database. If I also save the timer in the database, you have to make a query every second which is very bad practice.
I remembered something about WebSockets and tried to look into them. I actually managed to build a basic Hello World chat application, but don't really know how to implement this in my project. There is no place for it in the database (because of the queries), so I don't really know where to save that timer on the server. I'm also doubting if WebSockets are the way to go.
So are WebSockets the way to go and if it is, can you guys point me in the right direction on how to implement this? If not, can you advise me what I should do?
Thanks in advance!
From your question:
I understand that the objects you print in the screen are modified by
users in the application, and your aim is to live forward those
modifications to other active client instances of your application.
In that case, as you mention, I would point you to websockets. They are a great way to feed information directly to the client, so the client receives the update signals and modify the interface, with no need of user action.
In order to implement the logic to notify the client, I recommend using a push approach, but this is really depending on what kind of clients you'd like to notify, since the push world is still a bit tricky.
Further readings for this websocket based push implementation:
Question about Push Flags:
Difference between push and urgent flags in TCP
If your client runs in browser or mobile this question is nice to read:
How to send push notification to web browser?
Also html5 websockets:
http://www.websocket.org/aboutwebsocket.html
As a sidenote:
A nice architecture for client-server live communication is based on node.js together with socket.io library offering good performance and not really complex implementation if you know what you do.
I have client/server program, the client calls the server using [command], then I want server to call back just that one client with some data, or call some function on that one player without sending/calling to all other clients.
Does anybody know how to do it, and can you please show an example. I saw a function called SendToClientOfPlayer, but I cant find any example of how to use it, I dont even know if this is the right function to use.
u can use clientRPC
for example
[ClientRpc]
void RpcSendToClientOfPlayer(){
//do work
}
whenever u call it, it'll broadcast this function to all the clients
PS: make sure u have Rpc as prefix to the function
My scenario is this:
I have multiple webservers that:
need to communicate with the backend (IBus.Publish/IBus.Subscribe)
need to communicate with each-other (IBus.Publish/IBus.Subscribe)
Aside from the webservers, I have a number of windows services that consume the same messages.
In order to make this work, I have the webservers send messages to a central hub, which sole responsebility it is to wrap the message in a new message type and publish it to all subscribers.
Can I somehow avoid this, so I can publish the messages directly from the webservers?
EDIT (Added some code) - Current situation:
... WebServer
_bus.Send(new Message{Body="SomethingChanged"});
... Hub
public void Handle(Message message){
_bus.Publish(new WrappedMessage{Message = message})
}
... Handlers (WebServers, WindowsServices etc)
public void Handle(WrappedMessage message){
//Actually do important stuff
}
Wanted situation:
... WebServer
_bus.Publish(new Message{Body="SomethingChanged"};
... Handlers (WebServers, WindowsServices etc)
public void Handle(Message message){
//Do important stuff
}
Well, there isn't anything that technically prevents you from publishing messages inside your web application, and likewise there's nothing that prevents you from subscribing to those messages in all instances of the same web application. The question is whether you should :)
Without knowing the details of your problem, my immediate feeling is that you would be better off using some kind of shared persistent storage for whatever it is that you're trying to synchronize (a cache?), possibly using some kind of read replication if you'd like to scale out and make reads really fast.
Again, without knowing the details of your problem, I'll try and suggest something, and then you can see if that could inspire you into an even better solution... here goes:
Use MongoDB (possible as a replica set if you want to scale out your read operations) as the persistent storage of the thing you're caching
Whenever something happens in the web application, bus.Send a message to your backend
In your backend message handler, you update Mongo (which automatically will replicate to read slaves)
Whenever you need to query your data, you just query your Mongo set (using slaveOk=true whenever you can accept slightly stale values)
The reason I'm suggesting this alternative solution, is that web applications (at least in .NET land) have this funny transient nature where the IIS will dictate its lifecycle, and at any given time you can have n instances of it. This complicates matters if you keep state in it. This makes me think of the web application as a client, not a publisher.
A simpler solution is to keep state in something that does not come & go, e.g. a database. And the reason I'm suggesting Mongo is that my guess is that you're worried about being able to serve web requests fast, but since MongoDB is fairly easy to install as a replica set where read operations will be pretty fast (and, more importantly: horisontally scaleable), my guess is that this setup would make everything much simpler.
How does that sound?