我有这段代码。
这很好......我也创建了服务器端功能。
现在我想改变客户端代码,并使用AngularJS重新创建相同的功能。因为我只想在我的应用程序中添加这段代码一次,所以我认为应该使用服务或工厂(我认为服务更适合此目的)。
因为我是AngularJS的新手,对于如何使其工作有点困惑,我在互联网上搜索,但找不到有用的东西。有人能给出一些链接或一个简单的示例,说明如何使用Angular重建相同的代码吗?
$(function () {
//
// Initialize section
//
//
var hub = $.connection.hub,
platformHub = $.connection.PlatformHub,
tryingToReconnect = false,
firstConnection = true,
state = 'notconnected';
hub.url = 'http://localhost:80/signalr';
hub.logging = true;
//
// CLIENT METHODS SECTION
//
platformHub.client.hello = function (message) {
kendoConsole.log(message);
//Transient state variable from property bag
kendoConsole.log('Connection Started on : ' + platformHub.state.ConnectionStartedOn);
};
platformHub.client.greetings = function (message) {
kendoConsole.log(message);
};
//
// START HUB
//
//
function StartHub() {
try {
$('#connectionstatus').html('Connecting...');
hub.start({ transport: ['webSockets', 'longPolling', 'serverSentEvents', 'foreverFrame'] })
.done(function () {
tryingToReconnect = false;
state = 'connected';
//set connected status
$('#connectionstatus').html('connected!');
kendoConsole.log('transport = ' + hub.transport.name);
if (firstConnection) {
//send hello
$('#send').click(function () {
platformHub.server.hello();
});
//subscribe to groups
$('#subscribe').click(function () {
var v = $('#group').val();
platformHub.server.subscribe(v);
});
//handle error on server method.
//Each method is a promise and has a fail method
$('#error').click(function () {
try {
platformHub.server
.raiseError()
.done(function () {
kendoConsole.log('Raise error method called on server.');
})
.fail(function (error) {
kendoConsole.error('Failed: ' + error.message);
});
} catch (error) {
kendoConsole.error('Error: ' + error);
}
});
//get Time (With return value)
$('#getTime').click(function () {
try {
platformHub.server
.getServerTime()
.done(function (message) {
var _msg = "Current time = " + message.CurrentTime +
", utc = " + message.CurrentUtcTime +
', zone = ' + message.TimeZone.StandardName;
kendoConsole.log(_msg);
});
} catch (error) {
kendoConsole.error('Error: ' + error);
}
});
}
//set to fase in order not to create duplicate events for buttons
firstConnection = false;
})
.fail(function (error) {
kendoConsole.error('An error has occurred connecting: ' + error);
});
} catch (error) {
kendoConsole.error('An error has occurred connecting: ' + error);
}
}
//
// HANDLE EVENTS SECTION
//
//
hub
// TRying to reconnect event
//
.reconnecting(function () {
tryingToReconnect = true;
$('#connectionstatus').html('trying to reconnect....');
})
//
// state changed
//
.stateChanged(function (change) {
if (change.newState === $.connection.connectionState.disconnected) {
tryingToReconnect = true;
}
else { tryingToReconnect = false;}
})
//
// Rconnected event
//
.reconnected(function () {
tryingToReconnect = false;
state = 'connected';
$('#connectionstatus').html('reconnected');
})
//
// Disconnect event
//
//handle disconnect event. by default it will fire after 30 seconds
// https://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events
//error handling https://www.asp.net/signalr/overview/guide-to-the-api/platformHubs-api-guide-javascript-client#connectionlifetime
.disconnected(function () {
tryingToReconnect = true;
state = 'disconnected';
if (hub.lastError) {
$('#connectionstatus').html('disconnected! Reason=' + hub.lastError.message);
}
else {
$('#connectionstatus').html('disconnected!');
}
//trying to reconnect.
if (tryingToReconnect) {
setTimeout(function () {
StartHub();
}, 5000); // Restart connection after 5 seconds.
}
})
//
// Slow Connection Event
//
.connectionSlow(function () {
kendoConsole.log('We are currently experiencing difficulties with the connection.')
})
//
// Error event
//
.error(function (error) {
kendoConsole.error('An error occurred on the hub connection: ' + error);
});
//start hub
StartHub();
});
这很好......我也创建了服务器端功能。
现在我想改变客户端代码,并使用AngularJS重新创建相同的功能。因为我只想在我的应用程序中添加这段代码一次,所以我认为应该使用服务或工厂(我认为服务更适合此目的)。
因为我是AngularJS的新手,对于如何使其工作有点困惑,我在互联网上搜索,但找不到有用的东西。有人能给出一些链接或一个简单的示例,说明如何使用Angular重建相同的代码吗?