CometD taking more time in pushing messages - javascript

I am trying to implement CometD in our application. But it is taking more time compared to the existing implementation in our project. The existing system is taking time in milliseconds where as CometD is taking 2 seconds to push the message.
I am not sure where I am going wrong. Any guidance will help me lot.
My code:
Java script at client side
(function($)
{
var cometd = $.cometd;
$(document).ready(function()
{
function _connectionEstablished()
{
$('#body').append('<div>CometD Connection Established</div>');
}
function _connectionBroken()
{
$('#body').append('<div>CometD Connection Broken</div>');
}
function _connectionClosed()
{
$('#body').append('<div>CometD Connection Closed</div>');
}
// Function that manages the connection status with the Bayeux server
var _connected = false;
function _metaConnect(message)
{
if (cometd.isDisconnected())
{
_connected = false;
_connectionClosed();
return;
}
var wasConnected = _connected;
_connected = message.successful === true;
if (!wasConnected && _connected)
{
_connectionEstablished();
}
else if (wasConnected && !_connected)
{
_connectionBroken();
}
}
// Function invoked when first contacting the server and
// when the server has lost the state of this client
function _metaHandshake(handshake)
{
if (handshake.successful === true)
{
cometd.batch(function()
{
cometd.subscribe('/java/test', function(message)
{
$('#body').append('<div>Server Says: ' + message.data.eventID + ':'+ message.data.updatedDate + '</div>');
});
});
}
}
// Disconnect when the page unloads
$(window).unload(function()
{
cometd.disconnect(true);
});
var cometURL = "http://localhost:8080/cometd2/cometd";
cometd.configure({
url: cometURL,
logLevel: 'debug'
});
cometd.addListener('/meta/handshake', _metaHandshake);
cometd.addListener('/meta/connect', _metaConnect);
cometd.handshake();
});
})(jQuery);
Comet service class
#Listener("/service/java/*")
public void processMsgFromJava(ServerSession remote, ServerMessage.Mutable message)
{
Map<String, Object> input = message.getDataAsMap();
String eventId = (String)input.get("eventID");
//setting msg id
String channelName = "/java/test";
// Initialize the channel, making it persistent and lazy
bayeux.createIfAbsent(channelName, new ConfigurableServerChannel.Initializer()
{
public void configureChannel(ConfigurableServerChannel channel)
{
channel.setPersistent(true);
channel.setLazy(true);
}
});
// Publish to all subscribers
ServerChannel channel = bayeux.getChannel(channelName);
channel.publish(serverSession, input, null);
}
Is there any thing I need to change in server side code.

You have made your channel lazy, so a delay in message broadcasting is expected (that is what lazy channels are all about).
Please have a look at the documentation for lazy channels.
If you want immediate broadcasting don't set the channel as lazy.

Related

How can I kill a SignalR connection?

I am using SignalR to transfer data on a website. But SignalR should only be able to send data for a period of time and if the time period has passed the connection should be killed.
The Stop-Function $.connection.hub.stop() is cancelled if a request is still pending and is not completed. But this request should be forced to cancel no matter how much data has been sent.
How can I kill a SignalR-Connection?
As you can see in this Microsoft Documentation about Timeout and keepalive settings you can define the DisconnectTimeout in the options.
Example:
protected void Application_Start(object sender, EventArgs e)
{
// Make long-polling connections wait a maximum of 110 seconds for a
// response. When that time expires, trigger a timeout command and
// make the client reconnect.
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);
// Wait a maximum of 30 seconds after a transport connection is lost
// before raising the Disconnected event to terminate the SignalR connection.
GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);
// For transports other than long polling, send a keepalive packet every
// 10 seconds.
// This value must be no more than 1/3 of the DisconnectTimeout value.
GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);
RouteTable.Routes.MapHubs();
}
Edit: Since you want to kill the connection from the client no matter what, you are talking about a CancellationToken behavior but unfortunately this is still not supported in SignalR as you can see here and here, the team wants to do that to SignalR but still there is no news about it.
Please read this microsoft document about Hub lifetime event. You can change the default values for these settings, set them in Application_Start in your Global.asax file. But this way you can't full control client side. So you use javascript setTimeout function and pass the time form server end when a new user connect.it's may be GlobalHost.Configuration.DisconnectTimeout or any time you want. I give a full example with demo project. Actually i use this logic in a very large ticketing system for real-time holding ticket.(please read all inline comment)
Model:
public class MyModel
{
public int Id { get; set; }
public string Name { get; set; }
public static string Send(MyModel my)
{
//Do Somthing
return $"Data Sending to {my.Name}...";
}
public static string Stop(string name)
{
//Do Somthing
return $"ForceStop {name}.";
}
public static string Delete()
{
//Do Somthing
return "Deleted";
}
}
Hub:
[HubName("myHub")]
public class MyHub : Hub
{
int connectionTimeOut = 10;//sec
[HubMethodName("connect")]
public void Connect()
{
//apply logic if any when user connected or reload page
//set connection Time Out as you need
connectionTimeOut= 10;// GlobalHost.Configuration.DisconnectTimeout
Clients.Client(Context.ConnectionId).onNewUserConnected(connectionTimeOut);
}
[HubMethodName("startSendingServer")]
public void StartSending(int id, string name)//pass anything you need
{
//apply logic if any when start sending data
var my = new MyModel
{
Id = id,
Name = name
};
var status = MyModel.Send(my);//example
var result = new
{
status,
name
};
Clients.Client(Context.ConnectionId).startSendingClient(result);
}
[HubMethodName("forceStopServer")]
public void ForceStop(string name)//pass anything you need
{
//apply logic if any when force stop sending data
var status = MyModel.Stop(name);
Clients.Client(Context.ConnectionId).forceStopClint(status);
}
public override Task OnDisconnected(bool stopCalled)
{
//apply logic if any when connection Disconnected
var status = MyModel.Delete();//example
if (stopCalled)
{
// status=String.Format("Client {0} explicitly closed the connection.", Context.ConnectionId)
//your code here
}
else
{
// status=String.Format("Client {0} timed out .", Context.ConnectionId);
//your code here
//Clients.Client(Context.ConnectionId).onUserDisconnected(status);
}
return base.OnDisconnected(stopCalled);
}
}
TestView:
<div class="row">
<div class="col-md-12">
<h1> Status: <span id="hubStatus"></span></h1>
<br />
<h4> Countdown : <span id="counter"></span></h4>
<br />
<button id="btnHub" class="btn btn-primary btn-lg">Start Sending Data</button>
</div>
</div>
#section scripts{
<script src="~/Scripts/app/hub.js"></script>
}
hub.js:
var proxyTimer = null;
var sendTimeLimit = 1;//sec
var sessionTime = sendTimeLimit * 1000;
$(function () {
var myProxy = $.connection.myHub;
$.connection.hub.start().done(function () {
registerServerEvents(myProxy);
});
clientMethods(myProxy);
});
function registerServerEvents(proxyHub) {
proxyHub.server.connect();
$(document).on("click", "#btnHub", function (e) {
$("#hubStatus").html("Sending..");
$("#btnHub").text("Count Down Start...");
//Logic Before start sending data.
var id = 1;
var name = "AzR";
proxyHub.server.startSendingServer(id,name);
// $.connection.hub.disconnected(function () {
// setTimeout(function () { $.connection.hub.start(); }, 5000); // Restart connection after 5 seconds.
//});
$.connection.hub.disconnected(function () {
$("#hubStatus").html("Disconnected");// you can restart on here.
$("#btnHub").text("Stat Again after reload window");
});
});
}
function clientMethods(proxyHub) {
//proxyHub.on('onConnected', function (sendTimeLimit) {
// sendTimeLimit = sendTimeLimit;
//});
proxyHub.on('onNewUserConnected', function (serverItem) {
sendTimeLimit = serverItem;
sessionTime = sendTimeLimit * 1000;
});
proxyHub.on('startSendingClient', function (serverItem) {
//Logic after start sending data.
var name = serverItem.name;
var status = serverItem.status;
$("#hubStatus").html(status);
$("#counter").html(sendTimeLimit);
timeCounter();
startTimer(proxyHub, name );
});
proxyHub.on('forceStopClint', function (serverItem) {
clearClintPendingTask(serverItem);//Logic before proxy stop.
$("#btnHub").text("Force Stop...");
$.connection.hub.stop();
});
proxyHub.on('onUserDisconnected', function (serverItem) {
//Logic after proxy Disconnected (time out).
$("#hubStatus").html(serverItem);
$("#btnHub").text("Stat Again after reload window");
});
}
//Logic before proxy stop.
function clearClintPendingTask(status) {
//do all you need
$("#hubStatus").html(status);
stopTimer();
}
function startTimer(proxyHub,data) {
stopTimer();
proxyTimer = setTimeout(function () {
proxyHub.server.forceStopServer(data);
}, sessionTime);
}
function stopTimer() {
if (proxyTimer) {
clearTimeout(proxyTimer);
proxyTimer = null;
}
}
function timeCounter() {
var counter = sendTimeLimit;
var interval = setInterval(function () {
counter--;
$("#counter").html(counter);
if (counter == 0) {
//Do something
$("#counter").html("Countdown ended!");
// Stop the counter
clearInterval(interval);
}
}, 1000);
}
(Tested)
You need to define a timeout. On the server you can set DisconnectTimeout, like this:
GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromMinutes(30);
https://zzz.buzz/2016/05/11/setting-timeout-for-signalr-for-easier-debugging/
Updated Edit, please see Option 3 below. All the others are relying on timeout, I posted a forced disconnect.
If you are trying a Force Disconnect -- you can get the list of the Connected Users and call the ForceLogOut Function on the server side, I saw this somewhere on code project, I hope it helps. If you only want to forceLogout/kill some of the users, just loop through and kill that connection only.
Server Side
public class User
{
public string Name { get; set; }
public HashSet<string> ConnectionIds { get; set; }
}
public class ExtendedHub : Hub
{
private static readonly ConcurrentDictionary<string, User> ActiveUsers =
new ConcurrentDictionary<string, User>(StringComparer.InvariantCultureIgnoreCase);
public IEnumerable<string> GetConnectedUsers()
{
return ActiveUsers.Where(x => {
lock (x.Value.ConnectionIds)
{
return !x.Value.ConnectionIds.Contains
(Context.ConnectionId, StringComparer.InvariantCultureIgnoreCase);
}
}).Select(x => x.Key);
}
public void forceLogOut(string to)
{
User receiver;
if (ActiveUsers.TryGetValue(to, out receiver))
{
IEnumerable<string> allReceivers;
lock (receiver.ConnectionIds)
{
allReceivers = receiver.ConnectionIds.Concat(receiver.ConnectionIds);
}
foreach (var cid in allReceivers)
{
// ***************** log out/KILL connection for whom ever your want here
Clients.Client(cid).Signout();
}
}
}
}
Client Side
// 1- Save your connection variable when you start it, and later on you can use it to stop.
var myHubProxy = $.connection.myHub
// 2- Use it when you need to stop it, IF NOT YOU WILL GET AN ERROR
myHubProxy.client.stopClient = function() {
$.connection.hub.stop();
};
// With a button for testing
$('#SomeButtonKillSignalr').click(function () {
$.connection.hub.stop();
});
Updated with Option 3: based on request... the other solutions rely on time out, but you can also force it directly by disposing the connection yourself
I opened the SignalR code up, and inside you can see DisposeAndRemoveAsync the actual termination of a client connection.
1- You can modify or call DisposeAndRemoveAsync with your connection.
2- Then call RemoveConnection(connection.ConnectionId);
public async Task DisposeAndRemoveAsync(HttpConnectionContext connection)
{
try
{
// this will force it
await connection.DisposeAsync();
}
catch (IOException ex)
{
_logger.ConnectionReset(connection.ConnectionId, ex);
}
catch (WebSocketException ex) when (ex.InnerException is IOException)
{
_logger.ConnectionReset(connection.ConnectionId, ex);
}
catch (Exception ex)
{
_logger.FailedDispose(connection.ConnectionId, ex);
}
finally
{
// Remove it from the list after disposal so that's it's easy to see
// connections that might be in a hung state via the connections list
RemoveConnection(connection.ConnectionId);
}
}
Caution, do any clean up yourself when this is done.

Angular 8 - handling SSE reconnect on error

I'm working on an Angular 8 (with Electron 6 and Ionic 4) project and right now we are having evaluation phase where we are deciding whether to replace polling with SSE (Server-sent events) or Web Sockets. My part of the job is to research SSE.
I created small express application which generates random numbers and it all works fine. The only thing that bugs me is correct way to reconnect on server error.
My implementation looks like this:
private createSseSource(): Observable<MessageEvent> {
return Observable.create(observer => {
this.eventSource = new EventSource(SSE_URL);
this.eventSource.onmessage = (event) => {
this.zone.run(() => observer.next(event));
};
this.eventSource.onopen = (event) => {
console.log('connection open');
};
this.eventSource.onerror = (error) => {
console.log('looks like the best thing to do is to do nothing');
// this.zone.run(() => observer.error(error));
// this.closeSseConnection();
// this.reconnectOnError();
};
});
}
I tried to implement reconnectOnError() function following this answer, but I just wasn't able to make it work. Then I ditched the reconnectOnError() function and it seems like it's a better thing to do. Do not try to close and reconnect nor propagate error to observable. Just sit and wait and when the server is running again it will reconnect automatically.
Question is, is this really the best thing to do? Important thing to mention is, that the FE application communicates with it's own server which can't be accessed by another instance of the app (built-in device).
I see that my question is getting some attention so I decided to post my solution. To answer my question: "Is this really the best thing to do, to omit reconnect function?" I don't know :). But this solution works for me and it was proven in production, that it offers way how to actually control SSE reconnect to some extent.
Here's what I did:
Rewritten createSseSource function so the return type is void
Instead of returning observable, data from SSE are fed to subjects/NgRx actions
Added public openSseChannel and private reconnectOnError functions for better control
Added private function processSseEvent to handle custom message types
Since I'm using NgRx on this project every SSE message dispatches corresponding action, but this can be replaced by ReplaySubject and exposed as observable.
// Public function, initializes connection, returns true if successful
openSseChannel(): boolean {
this.createSseEventSource();
return !!this.eventSource;
}
// Creates SSE event source, handles SSE events
protected createSseEventSource(): void {
// Close event source if current instance of SSE service has some
if (this.eventSource) {
this.closeSseConnection();
this.eventSource = null;
}
// Open new channel, create new EventSource
this.eventSource = new EventSource(this.sseChannelUrl);
// Process default event
this.eventSource.onmessage = (event: MessageEvent) => {
this.zone.run(() => this.processSseEvent(event));
};
// Add custom events
Object.keys(SSE_EVENTS).forEach(key => {
this.eventSource.addEventListener(SSE_EVENTS[key], event => {
this.zone.run(() => this.processSseEvent(event));
});
});
// Process connection opened
this.eventSource.onopen = () => {
this.reconnectFrequencySec = 1;
};
// Process error
this.eventSource.onerror = (error: any) => {
this.reconnectOnError();
};
}
// Processes custom event types
private processSseEvent(sseEvent: MessageEvent): void {
const parsed = sseEvent.data ? JSON.parse(sseEvent.data) : {};
switch (sseEvent.type) {
case SSE_EVENTS.STATUS: {
this.store.dispatch(StatusActions.setStatus({ status: parsed }));
// or
// this.someReplaySubject.next(parsed);
break;
}
// Add others if neccessary
default: {
console.error('Unknown event:', sseEvent.type);
break;
}
}
}
// Handles reconnect attempts when the connection fails for some reason.
// const SSE_RECONNECT_UPPER_LIMIT = 64;
private reconnectOnError(): void {
const self = this;
this.closeSseConnection();
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
self.openSseChannel();
self.reconnectFrequencySec *= 2;
if (self.reconnectFrequencySec >= SSE_RECONNECT_UPPER_LIMIT) {
self.reconnectFrequencySec = SSE_RECONNECT_UPPER_LIMIT;
}
}, this.reconnectFrequencySec * 1000);
}
Since the SSE events are fed to subject/actions it doesn't matter if the connection is lost since at least last event is preserved within subject or store. Attempts to reconnect can then happen silently and when new data are send, there are processed seamlessly.

Socket.io event that waits for both players to have chosen answers

I am busy learning socket.io and trying to figure out how to make a bit of JS only fire off once both users have clicked their answers.
The code in question I am wanting to fire off is:
// Random number function
function randomIntFromInterval(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
// Check if correct + send to server
function correctAnswer() {
var correct = true;
socket.emit('playerCorrect', {answer: correct});
console.log(correct);
buttonRemover();
}
// Check if wrong + send to server
function incorrectAnswer () {
var wrong = false;
socket.emit('playerWrong', {answer: wrong});
buttonRemover();
}
socket.on ('updatePlayer', function (data) {
if (data.answer === true) {
console.log ('Player got it right! ' + data.answer);
}else if (data.answer === false) {
console.log ('Player got it wrong! ' + data.answer);
}
});
What those functions are sending data to the server to let the server know if the answer was correct or not.
socket.on('playerCorrect', function (data) {
io.sockets.emit('updatePlayer', data);
dataRequest();
});
socket.on('playerWrong', function (data) {
io.sockets.emit('updatePlayer', data);
dataRequest();
});
However I am only wanting this stuff to happen when both clients have clicked an option. Is there a way to track this?
For a single set of users, it would suffice to do:
// Global variables for maintaining game states
// TODO: Clear this when a new game is started
var nrecieved = 0;
var responses = {}; // Socket id to response
function finish(/* todo: maybe also pass in a game_id or one of the user ids to know which game is being played */){
// Loop through users in game and send them their responses
for(var id in responses){
if(responses.hasOwnProperty(id)){
// Send the response
io.to(id).emit('updatePlayer', responses[id]);
}
}
}
socket.on('playerCorrect', function (data) {
responses[socket.id] = data;
nrecieved++;
if(nrecieved == 2){
finish();
dataRequest();
}
});
socket.on('playerWrong', function (data) {
responses[socket.id] = data;
nrecieved++;
// Only respond if both responses received
if(nrecieved == 2){
finish();
dataRequest();
}
});
socket.id is a unique identifier assigned to each socket which may be useful. For multi-game performance, you will probably need to have more logic to maintain a list of which sockets/players belong to which games and then use that to direct messages to the correct pairs of users.

Extended execution does not seem to work in Windows 10 Universal JavaScript apps

I have a Windows 10 Universal app that's written in JavaScript. The app is a location tracker and needs to run in the background, and I am attempting to use the ExtendedExecution APIs to make that happen. I'm finding, though, that this works as advertised in a C#/XAML app, but does not work in a JavaScript app.
As an experiment, in Visual Studio 2015 I created a new C# project via File -> New -> Project -> Visual C# -> Blank App (Universal Windows) and kitted it out as follows:
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private Geolocator locator;
private ObservableCollection<string> coordinates = new ObservableCollection<string>();
private ExtendedExecutionSession session;
public MainPage()
{
this.InitializeComponent();
// Start geo locating
locator = new Geolocator();
locator.DesiredAccuracy = PositionAccuracy.High;
locator.DesiredAccuracyInMeters = 0;
locator.MovementThreshold = 0;
locator.PositionChanged += positionChanged;
coords.ItemsSource = coordinates;
// Request extended execution
RequestExtendedExecution();
}
private async void RequestExtendedExecution()
{
// Request extended execution via the ExtendedExecution API
session = new ExtendedExecutionSession();
session.Description = "Location Tracker";
session.Reason = ExtendedExecutionReason.LocationTracking;
session.Revoked += ExtendedExecutionSession_Revoked;
var result = await session.RequestExtensionAsync();
if (result == ExtendedExecutionResult.Allowed)
coordinates.Insert(0, "Extended execution SUCCESS");
else if (result == ExtendedExecutionResult.Denied)
coordinates.Insert(0, "Extended execution DENIED");
else
coordinates.Insert(0, "Extended execution unexpected return code");
}
private async void EndExtendedExecution()
{
if (session != null)
{
session.Dispose();
session = null;
}
}
private void ExtendedExecutionSession_Revoked(object sender, ExtendedExecutionRevokedEventArgs args)
{
var _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
coordinates.Insert(0, "Extended execution REVOKED: " + ((args.Reason == ExtendedExecutionRevokedReason.Resumed) ? "Resumed" : (args.Reason == ExtendedExecutionRevokedReason.SystemPolicy) ? "Resources" : "Unknown reason"));
});
EndExtendedExecution();
}
private void positionChanged(Geolocator sender, PositionChangedEventArgs args)
{
var coord = args.Position;
string position = string.Format("{0},{1}",
args.Position.Coordinate.Point.Position.Latitude,
args.Position.Coordinate.Point.Position.Longitude);
var _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
coordinates.Insert(0, position);
});
}
}
The markup is simply:
<ListView x:Name="coords" />
This absolutely works as expected. When I request the extended session, I get "Extended execution SUCCESS", when minimized the app continues to add coords to the ListView, and when returned to the foreground I get "Extended execution REVOKED: Resumed". Super duper. Back to Visual Studio 2015, I then created a new JavaScript project via File -> New -> Project -> JavaScript -> Blank App (Universal Windows) and implemented the same features as follows:
(function () {
"use strict";
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
var extendedExecution = Windows.ApplicationModel.ExtendedExecution;
var session = null;
var geolocator = null;
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize your application here.
// Start geo tracking
Windows.Devices.Geolocation.Geolocator.requestAccessAsync().done(
function (accessStatus) {
switch (accessStatus) {
case Windows.Devices.Geolocation.GeolocationAccessStatus.allowed:
geolocator = new Windows.Devices.Geolocation.Geolocator();
geolocator.ReportInterval = 1000;
// Subscribe to PositionChanged event to get updated tracking positions
geolocator.addEventListener("positionchanged", function (e) {
var coord = e.position.coordinate;
log("app.onactivated: coord = " + coord.point.position.latitude + ", " + coord.point.position.longitude, false, true, false);
});
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.denied:
log("Geolocator.requestAccessAsync: Access to location is denied.", false, true, false);
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.unspecified:
log("Geolocator.requestAccessAsync: Unspecified error.", false, true, false);
break;
}
},
function (err) {
log("Geolocator.requestAccessAsync: error " + err, false, true, false);
}
);
// Request extended execution
requestExtendedExecution();
} else {
// TODO: This application was suspended and then terminated.
// To create a smooth user experience, restore application state here so that it looks like the app never stopped running.
}
args.setPromise(WinJS.UI.processAll());
}
};
app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended. Save any state that needs to persist across suspensions here.
// You might use the WinJS.Application.sessionState object, which is automatically saved and restored across suspension.
// If you need to complete an asynchronous operation before your application is suspended, call args.setPromise().
log("app.oncheckpoint: application is about to be suspended");
};
function requestExtendedExecution() {
// If we already have an extended session, close it before requesting a new one.
if (session != null) {
session.close();
session = null;
}
// Request extended execution via the ExtendedExecution API
session = new extendedExecution.ExtendedExecutionSession();
session.description = "Background location tracking";
session.reason = extendedExecution.ExtendedExecutionReason.locationTracking;
session.onrevoked = function (args) {
log("requestExtendedExecution: Background mode revoked: " + args.reason);
requestExtendedExecution();
};
session.requestExtensionAsync().done(
function success() {
log("requestExtendedExecution: Successfully enabled background mode");
},
function error(error) {
log("requestExtendedExecution: Could not enable background mode: " + error);
}
);
}
function log (text) {
var now = new Date();
var timestamp = now.toLocaleDateString() + " " + now.toLocaleTimeString();
var outputDiv = document.getElementById("divOutput");
outputDiv.innerHTML = timestamp + " " + text + "<br/>" + outputDiv.innerHTML;
}
app.start();
})();
And the markup is:
<div id="divOutput"></div>
When I request the extended session, I still get "Extended execution SUCCESS", yay, but when I minimize the app, app.oncheckpoint gets called, the app gets suspended and there is no further activity until it returns to the foreground. I have also tried requesting the extended session from within app.oncheckpoint, but that has no effect either.
Anyone have some insight into this? Thanks in advance.
It works. The actual problem is your code doesn't listen on revoke event. It should be onrevoked. :)
And there are some small problems in your code.
Try the following:
function requestExtendedExecution() {
// Request extended execution via the ExtendedExecution API
session = new extendedExecution.ExtendedExecutionSession();
session.description = "Background location tracking";
session.reason = extendedExecution.ExtendedExecutionReason.locationTracking;
session.onrevoked = function (args) {
log("requestExtendedExecution: Background mode revoked: " + args.reason);
};
session.requestExtensionAsync().done(
function success() {
log("requestExtendedExecution: Successfully enabled background mode");
},
function error(error) {
log("requestExtendedExecution: Could not enable background mode: " + error);
}
);
}

Loop with socket.io terminates at the beginning

I'm building this function for upload to the server small tile images.
The client builds the tileBuffer and then calls the fireTiles function.
Here I would like to build a loop based on the tileBuffer.length. The server will handle the control. So, i emit StartAddTiles and I immediately called back from the server with the AnotherTile event. The debugger shows me I've been called by the server and I see the code going into the socket.on('AnotherTile'... sentence.
The problem is that when the code reaches the AddTile emit function, it stops there and nothing happens. The server does not receive the request and the loop is terminated there.
Where is the error in my code?
function fireTiles (tileBuffer, mapSelected) {
var tiles = tileBuffer.length;
var tBx = 0;
try
{
var socket = io.connect('http://myweb:8080/');
socket.emit('StartAddTiles', tiles, mapSelected);
socket.on('AnotherTile', function (tlN){
if (tlN < tiles) {
var data = tileBuffer[tlN]; //uso tlN per far comandare il server
tBx++; // debug purpose
socket.emit('AddTile', mapSelected, data, tBx);
} else {
// something went wrong
alert('Error calculating tiles');
return;
}
});
}
catch(err)
{
document.getElementById('status').innerHTML = err.message;
}
}
Here is the server side:
io.sockets.on('connection', function(client) {
console.log('Connecting....');
// controls are limited, this is just a beginning
// Initiate loop
client.on('StartAddTiles', function(tiles, mapSelected) {
var mapId = mapSelected;
mapLoading[mapId] = { //Create a new Entry in The mapLoading Variable
tilesToLoad : tiles,
tilesLoaded : 0
}
console.log('Start loading '+mapLoading[mapId].tilesToLoad+' tiles.');
// Ask for the first tile
client.emit('AnotherTile', mapLoading[mapId].tilesLoaded);
//
});
// client add new Tile/Tiles
client.on('addTile', function(mapSelected, data, tBx) {
var mapId = mapSelected;
mapLoading[mapId].tilesLoaded = ++1;
console.log('Adding tile '+mapLoading[mapId].tilesLoaded+' of '+mapLoading[mapId].tilesToLoad+' tBx '+tBx);
// insert Tile
db_manager.add_tiles(tileBuffer, function(result) {
if (mapLoading[mapId].tilesLoaded == mapLoading[mapId].tilesToLoad) { // full map loaded
mapLoading[mapId] = ""; //reset the buffer
client.emit('TilesOk', mapLoading[mapId].tilesLoaded);
} else {
console.log('requesting tile num: '+mapLoading[mapId].tilesLoaded);
client.emit('AnotherTile', mapLoading[mapId].tilesLoaded);
}
//
});
});
The event names are case sensitive, you should probably use AddTile instead of addTile on the server side too.

Categories

Resources