简体   繁体   English

使用node.js和socket.io发送异步信息

[英]Sending asynchronous information with node.js and socket.io

I have the following problem: I'm trying to send data collected by AMI (asterisk.io) using the sockets.io module, but I'm having difficulty to printing it in the html file. 我有以下问题:我正在尝试使用sockets.io模块发送AMI(asterisk.io)收集的数据,但是我很难将其打印在html文件中。 In the code below, the idea was to show it on the console as the data came in. I really do not know how to make this work, can anyone help me? 在下面的代码中,想法是在数据输入时将其显示在控制台上。我真的不知道如何使这项工作有效,有人可以帮助我吗? Thank you! 谢谢!

html file: html文件:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
  socket.on('data', function(data){
    console.log(data);
  });
</script>

asterisk.io with socket.io file: asterisk.io和socket.io文件:

var socket = require('../app');
require('events').EventEmitter.prototype._maxListeners = 1000;
var aio = require('asterisk.io');

var io = socket.io;
var ami = null;
ami = aio.ami(
    '192.168.0.7',
    5038,
    'admin',
    'admin'
);
ami.on('error', function(err){
    throw err;
});
io.on('connection', function(socket){
  ami.on('ready', function(){
    ami.action('SIPpeers', {}, function(data){
      ami.on('eventPeerEntry', function(data){
        socket.on('data', function(data){
          io.emit('data', data);
        });
      });
    });
  });
});

my app.js file: 我的app.js文件:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http').Server(app);
var cookieParser = require('cookie-parser');
var session = require('express-session');
var io = require('socket.io')(http);
exports.io = io;

(...)

//Execute main
http.listen(7777, function(){
  console.log("Running...");
});

[EDIT] After the help of Vaterrenanburg, this is the right code: [EDIT]在Vaterrenanburg的帮助下,这是正确的代码:

asterisk.io 星号

io.on('connection', function (socket) {
  ami.action('SIPpeers', {}, function(data){
    ami.on('eventPeerEntry', function(data){
      socket.emit("peer", data);
    });
  });
});

html file: html文件:

<script src="/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost');
    socket.on('peer', function (data) {
        (...)
        console.log(data);
    });
</script>

Currently, you have it set up so that the server emits the data event only when the client emits the data event. 当前,您已经对其进行了设置,以便服务器仅在客户端发出data事件时才发出data事件。 I believe from your description that you want the server to emit the data event to the client on the ami eventPeerEntry event. 我相信根据您的描述,您希望服务器在ami eventPeerEntry事件上向客户端发出data事件。 To do this, remove the subscription to the data event on the server. 为此,请删除服务器上data事件的预订。

io.on('connection', function(socket){
  ami.on('ready', function(){
    ami.action('SIPpeers', {}, function(data){
      ami.on('eventPeerEntry', function(data){
        io.emit('data', data);
      });
    });
  });
});

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

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