glowing-bear/js/websockets.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-02-18 00:49:42 +01:00
var weechat = angular.module('weechat', []);
weechat.factory('connection', ['$rootScope', function($scope) {
protocol = new Protocol();
2013-02-18 00:49:42 +01:00
var websocket = null;
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++) {
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");
websocket.binaryType = "arraybuffer"
2013-02-18 00:49:42 +01:00
websocket.onopen = function (evt) {
if (proto == "weechat") {
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", "&rArr; " + evt.data);
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) {
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
}]
);