简体   繁体   English

Socket.IO Angular服务器到服务器的连接

[英]Socket.IO Angular Server to Server Connection

Is it possible to do a simple socket.io connection on a client server running in gulp to a other server running in node? 是否可以在以gulp模式运行的客户端服务器上与以节点模式运行的其他服务器上进行简单的socket.io连接?

    .--------.      .----------.      .----------.
    |SERVER A|      |          |      | SERVER B |
    | (Gulp) |      |  CLIENT  |      | (Nodejs) |
    |  PORT  | <--> |   SIDE   | <--> |   PORT   |
    |  2772  |      |          |      |   8000   |
    '--------'      '----------'      '----------'

I tried to use the socket.io-client but I'm a bit confused.. but I use it like this: 我试图使用socket.io-client但是我有点困惑..但是我这样使用它:

Server.js Server.js

    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var port = process.env.PORT || 8000;
    var ioClient = require('socket.io-client');
    var client = ioClient.connect( "http://localhost:" + 2772 );

    io.on('connection', function(socket){
      console.log('a user connected')
      socket.on('disconnect', function(){
        console.log("a user disconnected")
      });
    });

     client.on('connection', function(socket){
       console.log('a user connected')
       client.on('disconnect', function(){
         console.log("a user disconnected")
       });
     });

In my Angular.. 在我的角度

App.js App.js

    var myApp = angular.module('ngclient', ['ngRoute','ui.bootstrap','ui.select','ngSanitize', 'btford.socket-io']);

Controller 控制者

    myApp.controller('dailydoseController', ['$scope', 'chatSocket', function($scope, chatSocket){


    }]);

Factory

myApp.factory('chatSocket', ['socketFactory', function(socketFactory){


   return socketFactory;
}]);

Its just the relevant... the entire code is too long.. Please help 它只是相关的...整个代码太长..请帮助

I'm not sure I've fully understood your question. 我不确定我是否完全理解您的问题。

In the client side (Angular in your case) you can use client implementation of socket.io (or with Angular component implementing socket-io client https://github.com/btford/angular-socket-io ). 在客户端(您的情况下为Angular),您可以使用socket.io的客户端实现(或与实现socket-io客户端https://github.com/btford/angular-socket-io的 Angular组件一起使用)。

In your node server, you should use server component of socket-io like the following one. 在节点服务器中,应使用socket-io的服务器组件,如下所示。

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
io.on('connection', function (socket) {
  // handle connection
})

Consider that even tough websocket are bidirectional, we still have a 'client' and the 'server'. 考虑到即使艰难的websocket都是双向的,我们仍然有一个“客户端”和“服务器”。 In your case the 'client' is Angular running on the browser and the 'server' is node. 在您的情况下,“客户端”是在浏览器上运行的Angular,而“服务器”是节点。 The node server cannot open the websocket directly with the client because the WebSockets API on the browser does not provide a way to listen for connections. 节点服务器无法直接通过客户端打开websocket,因为浏览器上的WebSockets API无法提供侦听连接的方法。

Hope it helps! 希望能帮助到你!

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

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