From d4b760fc04266370106944cc84b70f74d9bab70d Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 17 Feb 2013 18:49:42 -0500 Subject: [PATCH] can send messages to weechat --- js/websockets.js | 116 +++++++++++++++++++++++++---------------------- websockets.html | 14 ++++-- websockets.js | 46 ------------------- 3 files changed, 71 insertions(+), 105 deletions(-) delete mode 100644 websockets.js diff --git a/js/websockets.js b/js/websockets.js index eada7cc..746add8 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -1,62 +1,68 @@ -function WeechatCtrl ($scope) { +var weechat = angular.module('weechat', []); + +weechat.factory('connection', ['$rootScope', function($scope) { + + var websocket = null; + var doSend = function(message) { + msgs = message.replace(/[\r\n]+$/g, "").split("\n"); + for (var i = 0; i < msgs.length; i++) { + console.log("sent", "⇐ " + msgs[i]); + $scope.commands.push(msgs[i]); + } + websocket.send(message); + } + var connect = function (hostport, proto, password) { + websocket = new WebSocket("ws://" + hostport + "/weechat"); + + + websocket.onopen = function (evt) { + if (proto == "weechat") { + doSend("init password=" + password + "\ninfo version\ntest\n"); + } 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); + $scope.commands.push(evt.data); + $scope.$apply(); + } + websocket.onerror = function (evt) { + console.log("error", "ERROR: " + evt.data); + } + + this.websocket = websocket; + } + + var sendMessage = function(message) { + doSend(message); + } + return { + connect: connect, + sendMessage: sendMessage + } +}]); + +weechat.controller('WeechatCtrl', ['$rootScope', '$scope', 'connection', function ($rootScope, $scope, connection) { + $rootScope.commands = [] - $scope.status = { connected: false } $scope.hostport = "localhost:9001" $scope.proto = "weechat" $scope.password = "" + $scope.sendMessage = function() { + connection.sendMessage($scope.command); + $scope.command = ""; + }, + $scope.connect = function() { - + connection.connect($scope.hostport, $scope.proto, $scope.password); } - -} - -var output; -var hostport; -var proto; -var password; -function init() { - output = document.getElementById("output"); -// hostport = prompt("Enter hostname:port of WeeChat/relay", "hostname:5000") -// proto = prompt("Protocol (weechat / irc)", "weechat") -// password = prompt("Password (for relay)", "") - hostport = "localhost:9001" - proto = "weechat" - password = "" -/* websocket = new WebSocket("ws://" + hostport + "/weechat"); - websocket.onopen = function(evt) { onOpen(evt) }; - websocket.onclose = function(evt) { onClose(evt) }; - websocket.onmessage = function(evt) { onMessage(evt) }; - websocket.onerror = function(evt) { onError(evt) };*/ -} -function onOpen(evt) { - display("connected", "Connected to " + hostport); - if (proto == "weechat") { - doSend("init password=" + password + "\ninfo version\ntest\n"); - } else { - doSend("PASS " + password + "\r\nNICK test\r\nUSER test 0 * :test\r\n"); - } -} -function onClose(evt) { - display("disconnected", "Disconnected"); -} -function onMessage(evt) { - display("recv", "⇒ " + evt.data); -} -function onError(evt) { - display("error", "ERROR: " + evt.data); -} -function doSend(message) { - msgs = message.replace(/[\r\n]+$/g, "").split("\n"); - for (var i = 0; i < msgs.length; i++) { - display("sent", "⇐ " + msgs[i]); - } - websocket.send(message); -} -function display(class_name, message) { - var div = document.createElement("div"); - div.className = class_name; - div.innerHTML = message; - output.appendChild(div); -} -window.addEventListener("load", init, false); + }] +); diff --git a/websockets.html b/websockets.html index 3ba5e61..81d78c3 100644 --- a/websockets.html +++ b/websockets.html @@ -1,5 +1,5 @@ - + @@ -7,7 +7,7 @@
-
+
Connection
@@ -30,12 +30,18 @@
- +
-
+
+ {{ commands }} +
+ {{ command }} +
+ +
diff --git a/websockets.js b/websockets.js deleted file mode 100644 index 8c0a931..0000000 --- a/websockets.js +++ /dev/null @@ -1,46 +0,0 @@ -var output; -var hostport; -var proto; -var password; -function init() { - output = document.getElementById("output"); - hostport = prompt("Enter hostname:port of WeeChat/relay", "hostname:5000") - proto = prompt("Protocol (weechat / irc)", "weechat") - password = prompt("Password (for relay)", "") - websocket = new WebSocket("ws://" + hostport + "/weechat"); - websocket.onopen = function(evt) { onOpen(evt) }; - websocket.onclose = function(evt) { onClose(evt) }; - websocket.onmessage = function(evt) { onMessage(evt) }; - websocket.onerror = function(evt) { onError(evt) }; -} -function onOpen(evt) { - display("connected", "Connected to " + hostport); - if (proto == "weechat") { - doSend("init password=" + password + "\ninfo version\ntest\n"); - } else { - doSend("PASS " + password + "\r\nNICK test\r\nUSER test 0 * :test\r\n"); - } -} -function onClose(evt) { - display("disconnected", "Disconnected"); -} -function onMessage(evt) { - display("recv", "⇒ " + evt.data); -} -function onError(evt) { - display("error", "ERROR: " + evt.data); -} -function doSend(message) { - msgs = message.replace(/[\r\n]+$/g, "").split("\n"); - for (var i = 0; i < msgs.length; i++) { - display("sent", "⇐ " + msgs[i]); - } - websocket.send(message); -} -function display(class_name, message) { - var div = document.createElement("div"); - div.className = class_name; - div.innerHTML = message; - output.appendChild(div); -} -window.addEventListener("load", init, false);