Angularjs聊天
啟用網絡,iOS和Android應用程序的消息經驗。即將推出Angularjs和最佳框架Ionic,PubNub,Phonegap
npm install angular-chatbower install angular-chat在模板中包括角聊天文件。
< script src =" bower_components/angular/angular.js " > </ script >
< script src =" bower_components/rltm/web/rltm.js " > </ script >
< script src =" bower_components/angular-chat/angular-chat.js " > </ script > var chat = angular . module ( 'BasicChat' , [ 'chat' ] ) ; 為了使用AngularJs-Chat,您必須配置與實時服務的連接。該庫包含rltm.js作為依賴項,可讓您輕鬆地在socket.io和pubnub等實時服務提供商之間切換。我們建議使用PubNub建立以快速入門並擴展到無窮大。
angular . module ( 'chat' ) . constant ( 'config' , {
rltm : {
service : "pubnub" ,
config : {
"publishKey" : "demo" ,
"subscribeKey" : "demo"
}
}
} ) ; 聊天模塊公開了一個稱為Messages的對象,其中包括send和receive方法。
chat . controller ( 'chat' , [ 'Messages' , '$scope' , function ( Messages , $scope ) {
// Message Inbox
$scope . messages = [ ] ;
// Receive Messages
Messages . receive ( function ( message ) {
$scope . messages . push ( message ) ;
} ) ;
// Send Messages
$scope . send = function ( ) {
Messages . send ( {
data : $scope . textbox
} ) ;
} ;
} ] ) ;在此控制器中,我們在$scope.messages中保留消息的列表,並每次調用Messages.receive()時都會按下新消息。
要通過Internet發送消息,我們使用Messages.send()方法,然後將其附加到``$ scope.send()````````````````````````````````````````````````
在我們看來,我們使用$scope.send()方法和$scope.messages變量。
< div ng-app =" BasicChat " >
< div ng-controller =" chat " >
< div ng-repeat =" message in messages " >
< strong > {{message.user.name}}: </ strong >
< span > {{message.data}} </ span >
</ div >
< form ng-submit =" send() " >
< input ng-model =" textbox " >
</ form >
</ div >
</ div >為此用戶設置一些標識。
Messages . user ( { id : MY_USER_ID , name : sillyname ( ) } ) ;向另一個用戶發送消息。
Messages . send ( { to : target_user_id , data : message_body } ) ;如果您想要瞬態的隨機用戶ID ...您可以將用戶列表發佈到“全局”頻道,並接收到上網的每個用戶。
請訪問/examples/basic/index.html ,以獲取每個訪客可以聊天的聊天室的示例。
請訪問/examples/support-chat/index.html和/examples/support-chat/admin.html ,以獲取嵌入式支持類型聊天室的示例。頁面index.html只能在admin.html上與用戶聊天。該頁面admin.html為index.html上的每個新用戶創建一個聊天室的新實例。