Socket.io无法在节点服务器上运行

时间:2017-09-21 05:23:05

标签: node.js reactjs express socket.io

我正在运行服务器,它需要做的就是连接到第三方公共socket.io流。我有它在我的反应应用程序中工作,但无法让它在我的服务器上工作。我正在使用node和express。当我运行时,我没有错误。这是我的代码

 initializeApp() {
this.platform.ready().then(() => {
  this.storage.get('vAuthToken').then((val) => {
    if (val == null) {
    this.menuCtrl.enable(false, 'authenticated');
    this.menuCtrl.enable(true, 'unauthenticated');
  }
  else {
    this.menuCtrl.enable(true, 'authenticated');
    this.menuCtrl.enable(false, 'unauthenticated');
  }
  });
 });
 }
 this.authenticated = [
            { title: 'Welcome', component: SlidesPage },
            { title: 'Home', component: HomePage },
            { title: 'House & Lot', component: HousePage },
            { title: 'Condominium', component: CondoPage },
            { title: 'Apartment', component: ApartmentPage },
            { title: 'Hotel', component: HotelPage }
          ];
          this.unauthenticated = [
            { title: 'Dashboard', component: SellerPage },
            { title: 'My Properties', component: SellerPage },
            { title: 'Properties', component: HomePage },
            { title: 'House & Lot', component: HousePage },
            { title: 'Condominium', component: CondoPage },
            { title: 'Apartment', component: ApartmentPage },
            { title: 'Hotel', component: HotelPage }
          ];

2 个答案:

答案 0 :(得分:0)

刚刚检查了我的代码,我做了类似下面的代码,它与你的代码有点不同。不知道它是否是你的解决方案,但也许这可以帮助你。

在单独的socket-io.js

module.exports = function (app, server) {
     var socketIO = require('socket.io').listen(server, {'transports': ['websocket']});
     global.socketIO = socketIO;

     socketIO.set('origins', '*:*');

     socketIO.sockets.on('trades', function (tradeMsg) {
         console.log("worked");
     });
 };

然后在我www之后var server = http.createServer(app);我得到了:

require('socket-io')(app, server);

答案 1 :(得分:0)

您可以尝试使用socket.io-client安装npm

npm install socket.io-client --save

然后尝试运行以下代码。它应该工作正常。

var express = require("express");
var app = express();
var port = 3000;

var socket = require('socket.io-client')('http://socket.coincap.io');


socket.on('trades', function(tradeMsg) {

    console.log("worked");

});



app.listen(port, () => {
   console.log("Server listening on port " + port);
});