简体   繁体   English

Ionic / Angular和Socket.IO-在收到Socket.IO事件后显示弹出窗口

[英]Ionic/Angular and Socket.IO - Show popup upon Socket.IO event received

I'm trying to build a real-time mobile application utilizing the Ionic Framework (AngularJS) and Socket.IO. 我正在尝试使用Ionic Framework(AngularJS)和Socket.IO构建实时移动应用程序。

Coming from a jQuery background, I'm kind of new to the whole Ionic/Angular application design and structure (eg, controllers, services, etc.). 来自jQuery背景,对于整个Ionic / Angular应用程序的设计和结构(例如,控制器,服务等)来说,我还是一个新手。 So I was wondering, how should my application be structured, or what is the best/ideal way of structuring my application? 所以我想知道,应该如何构造我的应用程序,或者构造我的应用程序的最佳/理想方式是什么?

More specifically, in an Ionic/Angular-structured application, where should my usual Socket.IO code go? 更具体地说,在Ionic / Angular结构的应用程序中,我通常的Socket.IO代码应该放在哪里? I've seen one component ( https://github.com/btford/angular-socket-io ) which (as I understand) encapsulates Socket.IO code within an Angular factory. 我已经看到了一个组件( https://github.com/btford/angular-socket-io )(据我了解),该组件将Socket.IO代码封装在Angular工厂中。 Is this the way to go? 这是要走的路吗?

What I really want to achieve are the following: 我真正想要实现的目标如下:

  1. Open an (Ionic) popup when a Socket.IO event is received and 收到Socket.IO事件并打开时,打开一个(Ionic)弹出窗口
  2. emit/send a Socket.IO event/message upon a UI event (eg, button click). 在UI事件(例如,单击按钮)时发出/发送Socket.IO事件/消息。

How should my Ionic/Angular code structure look like to achieve this? 我的Ionic / Angular代码结构应该如何实现? That is, which code goes into which component/controller/service/factory? 也就是说,哪个代码进入哪个组件/控制器/服务/工厂?

Even a high-level description of the structure would do as a starting point. 甚至对结构的高级描述都可以作为起点。

As an example for you, Here's how I did it with a ChatCtrl controller. 作为您的示例,这是我使用ChatCtrl控制器进行操作的方法。

 <!-- app.js -->
 <script>
   angular.module('chatApp', ['ngAnimate'])

   .controller('ChatCtrl', ['$scope', '$http', function($scope, $http) {

     var socket = io.connect();

     // on connect, grab username
     socket.on('connect', function(data) {
       nickname = prompt("What's your name?");
       socket.emit('join', nickname);
     });

     socket.on('statusConnected', function(data) {
       $scope.status = data;
       $scope.$apply($scope.status);
     });

   }]);
 </script>

My popup is a JavaScript nickname = prompt("What's your name?") but you could probably trigger a modal or whatever other popup you want with some more JS 我的弹出窗口是一个JavaScript nickname = prompt("What's your name?")但是您可能会触发一个modal nickname = prompt("What's your name?")或使用其他JS弹出任何其他想要的弹出窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM