我如何连接到 Node.js 中的星号
Posted
技术标签:
【中文标题】我如何连接到 Node.js 中的星号【英文标题】:How can i connect to asterisk in Node.js 【发布时间】:2013-09-22 18:31:57 【问题描述】:我想通过套接字编程连接到星号我该怎么做? 我知道我们可以使用很多模块,但我不想使用主题。 我读了page 并想知道如何通过套接字编程连接到 Node.js 中的星号? 此代码是 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);
【问题讨论】:
【参考方案1】:使用这个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 连接到我的 redshift 集群?