gpt4 book ai didi

node.js - 我如何连接到 Node.js 中的 Asterisk

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:10 24 4
gpt4 key购买 nike

我想通过套接字编程连接到 Asterisk ,我该怎么做?我知道有很多模块可以使用,但我不想使用主题。我读过这个page并想知道如何通过套接字编程连接到 Node.js 中的 Asterisk ?此代码是 TCP 示例:

var net = require('net');

var HOST = '127.0.0.1';
var PORT = 6969;

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {

// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {

console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
sock.write('You said "' + data + '"');

});

// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

最佳答案

使用这个article :我们可以在node中使用socket编程。此示例代码用于通过TCP连接。

var net = require('net');
var port = 5038;
var host = "IP";
var username = "User";
var password = "Pass";
var CRLF = "\r\n";
var END = "\r\n\r\n";
var client = new net.Socket();

client.connect(port, host, function () {

console.log('CONNECTED TO: ' + host + ':' + port);
var obj = { Action: 'Login', Username: username, Secret: password};
obj .ActionID =1;
var socketData = generateSocketData(obj);
console.log('DATA: ' + socketData);
client.write(socketData, 'ascii');

});
generateSocketData = function(obj) {
var str = '';
for (var i in obj) {
console.log('obj[i]:'+obj[i]);
str += (i + ': ' + obj[i] + CRLF);
}
return str + CRLF;
};
client.on('data', function (data) {
console.log('New Event Recived');
console.log('******************************');
console.log('DATA: ' + data);


});

client.on('close', function () {
console.log('Connection closed');
});

关于node.js - 我如何连接到 Node.js 中的 Asterisk ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18947364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com