【问题标题】:Why my socket.io can not work in ionic?为什么我的 socket.io 不能在 ionic 中工作?
【发布时间】:2017-01-27 05:10:01
【问题描述】:

为什么我的 socket.io 不能在 ionic 中工作?我有一个简单的项目,我只是在 index.html 中编辑并在 js 文件夹中添加 index.js 以尝试套接字工作。

这是我的 index.html :

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link rel="manifest" href="manifest.json">

    <!-- un-comment this code to enable service worker
    <script>
      if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('service-worker.js')
          .then(() => console.log('service worker installed'))
          .catch(err => console.log('Error', err));
      }
    </script>-->

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
    <script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
      app.initialize();

      document.addEventListener('deviceready', function() {
        socket.on('connect', function() {
          socket.on('text', function(text) {
            alert(text);
           });
         });
      });
    </script>
  </head>
  <body ng-app="starter">
    <!--
      The nav bar that will be updated as we navigate between views.
    -->
    <ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>
    <!--
      The views will be rendered in the <ion-nav-view> directive below
      Templates are in the /templates folder (but you could also
      have templates inline in this html file if you'd like).
    -->
    <ion-nav-view></ion-nav-view>
  </body>
</html>

这是我的 index.js:

var server = require('http').createServer();
var io = require('socket.io')(server);

io.sockets.on('connection', function (socket) {
    console.log('socket connected');

    socket.on('disconnect', function () {
        console.log('socket disconnected');
    });

    socket.emit('text', 'Your socket is successed');
});

server.listen(3000);

我正在使用 ionic serve 和这样的错误输出运行我的项目:

enter image description here

【问题讨论】:

标签: javascript angularjs cordova ionic-framework socket.io


【解决方案1】:

你必须像这样在客户端初始化套接字:

var socket = io("yourDomainName");

还要确保您使用 socket.io-client 包作为客户端 socket.io.js

【讨论】:

  • 喜巴拉..如何实现你的代码?对不起,我是套接字的新手 :)
猜你喜欢
  • 2019-12-21
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 2012-01-13
  • 2021-02-24
  • 2013-01-03
相关资源
最近更新 更多