安装好环境,请参考ionic环境搭建之windows篇 和 ionic环境搭建之OS X篇 。

服务器端的搭建参考socket io官网,里面有非常详细的描述,按照步骤下来,最终可以在localhost进行模拟聊天。

下面是手机端的说明。

  • 引入socket.io.js:
<script src="js/socket.io.js"></script>
  • 定义Chats tab:
  <!-- Chats Tab -->
<ion-tab title="Chats" icon-off="ion-ios-chatboxes-outline" icon-on="ion-ios-chatboxes" href="#/tab/chats">
<ion-nav-view name="tab-chats"></ion-nav-view>
</ion-tab>
  •  定义tab-chat.html:
<ion-view view-title="Chats">
<ion-content overflow-scroll="true" style="overflow: hidden"> <ion-scroll zooming="true" direction="y" style=" height:500px;" >
<ion-list>
<uib-alert ng-repeat="msgInfo in messages" type="{{msgInfo.type}}" close="closeAlert($index)">{{msgInfo.msgType}}: {{msgInfo.msg}}</uib-alert>
</ion-list>
</ion-scroll> <div class="bar bar-footer">
<label class="item item-input" style=" width:85%">
<input type="text" placeholder="please add your message here" ng-model="msg"></input>
</label>
<button class="button button-positive" ng-click="send(msg)">Send</button> </div>
</ion-content>
</ion-view>
  • 在app.js中定义chats的state:
  .state('tab.chats', {
url: '/chats',
views: {
'tab-chats': {
templateUrl: 'templates/tab-chats.html',
controller: 'ChatsCtrl'
}
}
})
  •  定义ChatsCtrl:
.controller('ChatsCtrl', function ($scope,mediaPlayer, chats) {
$scope.messages = []; chats.on('chat message', function (msg) {
var msgInfo = { msg: msg, type: 'warning',msgType:"Receiver" }
$scope.messages.push(msgInfo);
console.log('receive msg from others: ' + msg);
}); $scope.send = function (msg) {
var msgInfo = { msg: msg, type: 'success', msgType: "Sender" }
$scope.messages.push(msgInfo);
console.log('start to send msg: ' + msg);
chats.emit('chat message', msg);
}; $scope.closeAlert = function (index) {
$scope.messages.splice(index, 1);
};
})
  • 实现factory:
var baseUrl = 'http://localhost:3000/';

.factory('chats', function socket($rootScope) {
var socket = io.connect(baseUrl);
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
})
}
};
})

参考资料:

https://github.com/jbavari/ionic-socket.io-redis-chat
http://jbavari.github.io/blog/2014/06/17/building-a-chat-client-with-ionic/
http://socket.io/get-started/chat/

最新文章

  1. html5 兼容火狐 ev的事件
  2. ios UIView autoresizingSubview 属性
  3. __iter__
  4. Jquery中$(document).ready()与传统JavaScript中的window.onload方法的区别(2016/8/3)
  5. HTML5探索之datalist研究
  6. Android控件之EditText(输入文本框控件)
  7. 一份C++线程池的代码,非常实用
  8. 设计模式 工厂-Factory
  9. 什么是WEB服务器?
  10. C++赋值运算符函数
  11. iOS9开发之新增通知行为详解
  12. sicily 1007 To and Fro (基地称号)
  13. 【二分贪心】Bzoj3969 [WF2013] Low Power
  14. python docx文档转html页面
  15. Springboot2新特性概述
  16. vim实现实时自动保存
  17. [工作积累] shadow map问题汇总
  18. C# 实现Remoting双向通信
  19. Python -- 网络编程 -- 简单抓取网页
  20. 设计模式 笔记 单例模式 Singleton

热门文章

  1. 策略与计费控制(PCC)流程与信令流程
  2. whereis libjpeg.so.7
  3. iOS wkwebview https 加载不受信用的站点
  4. MVC 登陆鉴权
  5. 认识学习MVC这家伙
  6. 灯塔AOI简易实现
  7. 协程《二》greenlet模块
  8. 老程序员解Bug的通用套路
  9. MongDB集群部署
  10. 最短路径SPFA算法(邻接表存法)