01.
public
async Task ConnectAsync(MobileServiceUser user)
02.
{
03.
_connection =
new
HubConnection(App.MobileService.ApplicationUri.AbsoluteUri);
04.
DebugTextWriter writer =
new
DebugTextWriter();
05.
_connection.TraceWriter = writer;
06.
_connection.TraceLevel = TraceLevels.All;
07.
_connection.Closed += () => writer.WriteLine(
"hubConnection.Closed"
);
08.
_connection.ConnectionSlow += () => writer.WriteLine(
"hubConnection.ConnectionSlow"
);
09.
_connection.Error += (error) => writer.WriteLine(
"hubConnection.Error {0}: {1}"
, error.GetType(), error.Message);
10.
_connection.Reconnected += () => writer.WriteLine(
"hubConnection.Reconnected"
);
11.
_connection.Reconnecting += () => writer.WriteLine(
"hubConnection.Reconnecting"
);
12.
_connection.StateChanged += (change) => writer.WriteLine(
"hubConnection.StateChanged {0} => {1}"
, change.OldState, change.NewState);
13.
14.
if
(user !=
null
)
15.
{
16.
_connection.Headers[
"x-zumo-auth"
] = user.MobileServiceAuthenticationToken;
17.
}
18.
else
19.
{
20.
_connection.Headers[
"x-zumo-application"
] = App.MobileService.ApplicationKey;
21.
}
22.
23.
_proxy = _connection.CreateHubProxy(
"SosThailandSignalRHub"
);
24.
25.
_proxy.On(
"AcceptJob"
, () => SignalR_OnAcceptJob());
26.
_proxy.On(
"CloseJob"
, () => SignalR_OnFinishJob());
27.
_proxy.On<Log>(
"SendLog"
, (log) => SignalR_OnReceiveLog(log));
28.
29.
await _connection.Start();
30.
}
31.
32.
private
async
void
SignalR_OnReceiveLog(BiddingLog log)
33.
{
34.
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal,
delegate
35.
{
36.
if
(
this
.ReceiveLog !=
null
)
37.
{
38.
this
.ReceiveLog(
this
,
new
SosLogEventArgs(log));
39.
}
40.
});
41.
}
42.
43.
private
async
void
SignalR_OnAcceptJob()
44.
{
45.
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal,
delegate
46.
{
47.
if
(
this
.AcceptJob !=
null
)
48.
{
49.
this
.AcceptJob(
this
,
new
EventArgs());
50.
}
51.
});
52.
}
53.
54.
private
async
void
SignalR_OnFinishJob()
55.
{
56.
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal,
delegate
57.
{
58.
if
(
this
.FinishJob !=
null
)
59.
{
60.
this
.FinishJob(
this
,
new
EventArgs());
61.
}
62.
});
63.
}