在 AngularJS 中创建 TCP 套接字 [英] Creating a TCP Socket in AngularJS
问题描述
在 PHP 中很容易创建、连接与 TCP 套接字的交互(http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP,客户端部分).
In PHP it is easy to create, connect an interact with a TCP socket (http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP, the Client part).
我怎样才能在 AngularJS 中做到这一点?在以下站点上尝试了该模块:https://github.com/BHSPitMonkey/angular-net-sockets,但失败了.这个网站上的一个例子会对我有帮助.
How can I accomplish this in AngularJS? Tried the module on following site: https://github.com/BHSPitMonkey/angular-net-sockets, but failed. An example on this site would have been helpful to me.
编辑
如何在我的控制器中使用它?我收到错误消息:工厂必须返回一个值".
How can I use that in my controller? I'm getting the error: 'factory must return a value'.
这就是我想要完成的:
app.controller('controller', ['$scope', 'netsockets', function ($scope, netsockets) {
$scope.username = "test";
$scope.send_socket = function(){
socket.connect('1.1.1.1','1111');
socket.sendData('hello world');
socket.disconnect();
socket.close();
};
}]);
这是我的工厂:
factory('netsockets', function(NetSocket) {
return {
init: function(){
var socket = new NetSocket({
onConnect: function(){
alert('I\'m connected!');
},
onDisconnect: function(){
alert('I\'m disconnected!');
}
});
}
}
});
推荐答案
angular.module('myApp.services', []).
factory('MyService', function(NetSocket) {
var socket = new NetSocket({
onConnect: function() {
alert('I\'m connected!');
},
onDisconnect: function() {
alert('I\'m disconnected!');
}
});
socket.connect('1.1.1.1','1111');
socket.sendData('hello world');
socket.disconnect();
socket.close();
});
编辑
您的工厂不退货.试试这个:
your factory does not return anything. Try with this:
factory('netsockets', function(NetSocket) {
return new NetSocket({
onConnect: function(){
alert('I\'m connected!');
},
onDisconnect: function(){
alert('I\'m disconnected!');
}
});
});
这篇关于在 AngularJS 中创建 TCP 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!