2013-02-18 00:49:42 +01:00
|
|
|
var weechat = angular.module('weechat', []);
|
|
|
|
|
|
|
|
weechat.factory('connection', ['$rootScope', function($scope) {
|
2013-02-24 20:44:03 +01:00
|
|
|
protocol = new Protocol();
|
2013-02-18 00:49:42 +01:00
|
|
|
var websocket = null;
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
var doSend = function(message) {
|
|
|
|
msgs = message.replace(/[\r\n]+$/g, "").split("\n");
|
|
|
|
for (var i = 0; i < msgs.length; i++) {
|
2013-02-24 20:44:03 +01:00
|
|
|
console.log('=' + msgs[i] + '=');
|
|
|
|
$scope.commands.push("SENT: " + msgs[i]);
|
2013-02-18 00:49:42 +01:00
|
|
|
}
|
|
|
|
websocket.send(message);
|
|
|
|
}
|
|
|
|
var connect = function (hostport, proto, password) {
|
|
|
|
websocket = new WebSocket("ws://" + hostport + "/weechat");
|
2013-02-24 20:44:03 +01:00
|
|
|
websocket.binaryType = "arraybuffer"
|
2013-02-18 00:49:42 +01:00
|
|
|
|
|
|
|
websocket.onopen = function (evt) {
|
|
|
|
if (proto == "weechat") {
|
2013-07-20 20:23:09 +02:00
|
|
|
doSend("init compression=off\nversion\n");
|
2013-02-18 00:49:42 +01:00
|
|
|
} else {
|
|
|
|
doSend("PASS " + password + "\r\nNICK test\r\nUSER test 0 * :test\r\n");
|
|
|
|
}
|
|
|
|
$scope.connected = true;
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
websocket.onclose = function (evt) {
|
|
|
|
console.log("disconnected", "Disconnected");
|
|
|
|
$scope.connected = false;
|
|
|
|
}
|
|
|
|
websocket.onmessage = function (evt) {
|
|
|
|
console.log("recv", "⇒ " + evt.data);
|
2013-02-24 20:44:03 +01:00
|
|
|
protocol.setData(evt.data);
|
|
|
|
console.log(protocol.parse());
|
|
|
|
$scope.commands.push("RECV: " + evt.data + " TYPE:" + evt.type) ;
|
|
|
|
data = evt.data;
|
2013-02-18 00:49:42 +01:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
websocket.onerror = function (evt) {
|
|
|
|
console.log("error", "ERROR: " + evt.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.websocket = websocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sendMessage = function(message) {
|
2013-02-24 20:44:03 +01:00
|
|
|
message = message + "\n"
|
2013-02-18 00:49:42 +01:00
|
|
|
doSend(message);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
connect: connect,
|
|
|
|
sendMessage: sendMessage
|
|
|
|
}
|
|
|
|
}]);
|
|
|
|
|
|
|
|
weechat.controller('WeechatCtrl', ['$rootScope', '$scope', 'connection', function ($rootScope, $scope, connection) {
|
|
|
|
$rootScope.commands = []
|
2013-02-16 19:18:14 +01:00
|
|
|
|
|
|
|
$scope.hostport = "localhost:9001"
|
|
|
|
$scope.proto = "weechat"
|
|
|
|
$scope.password = ""
|
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
$scope.sendMessage = function() {
|
|
|
|
connection.sendMessage($scope.command);
|
|
|
|
$scope.command = "";
|
|
|
|
},
|
2013-02-16 19:18:14 +01:00
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
$scope.connect = function() {
|
|
|
|
connection.connect($scope.hostport, $scope.proto, $scope.password);
|
2013-02-16 19:18:14 +01:00
|
|
|
}
|
2013-02-18 00:49:42 +01:00
|
|
|
}]
|
|
|
|
);
|