can send messages to weechat

This commit is contained in:
David Cormier 2013-02-17 18:49:42 -05:00
parent 251d2ae5a9
commit d4b760fc04
3 changed files with 71 additions and 105 deletions

View file

@ -1,62 +1,68 @@
function WeechatCtrl ($scope) {
var weechat = angular.module('weechat', []);
$scope.status = { connected: false }
$scope.hostport = "localhost:9001"
$scope.proto = "weechat"
$scope.password = ""
$scope.connect = function() {
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", "&lArr; " + msgs[i]);
$scope.commands.push(msgs[i]);
}
websocket.send(message);
}
var connect = function (hostport, proto, password) {
websocket = new WebSocket("ws://" + hostport + "/weechat");
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);
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();
}
function onClose(evt) {
display("disconnected", "Disconnected");
websocket.onclose = function (evt) {
console.log("disconnected", "Disconnected");
$scope.connected = false;
}
function onMessage(evt) {
display("recv", "&rArr; " + evt.data);
websocket.onmessage = function (evt) {
console.log("recv", "&rArr; " + evt.data);
$scope.commands.push(evt.data);
$scope.$apply();
}
function onError(evt) {
display("error", "ERROR: " + evt.data);
websocket.onerror = function (evt) {
console.log("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", "&lArr; " + msgs[i]);
this.websocket = websocket;
}
websocket.send(message);
var sendMessage = function(message) {
doSend(message);
}
function display(class_name, message) {
var div = document.createElement("div");
div.className = class_name;
div.innerHTML = message;
output.appendChild(div);
return {
connect: connect,
sendMessage: sendMessage
}
window.addEventListener("load", init, false);
}]);
weechat.controller('WeechatCtrl', ['$rootScope', '$scope', 'connection', function ($rootScope, $scope, connection) {
$rootScope.commands = []
$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);
}
}]
);

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html ng-app>
<html ng-app="weechat">
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="js/angular.min.js"></script>
@ -7,7 +7,7 @@
</head>
<body>
<div ng-controller="WeechatCtrl">
<div ng-hide="status.connected">
<div ng-hide="connected">
<form class="form-horizontal">
<legend>Connection</legend>
<div class="control-group">
@ -30,12 +30,18 @@
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-primary">Connect!</button>
<button class="btn btn-primary" ng-click="connect()">Connect!</button>
</div>
</div>
</form>
</div>
<div ng-show="status.connected">
<div ng-show="connected">
{{ commands }}
<div ng-repeat="command in commands">
{{ command }}
</div>
<input type="text" ng-model="command"></input>
<button class="btn btn-primary" ng-click="sendMessage()">Send</button>
</div>
</div>
</body>

View file

@ -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", "&rArr; " + 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", "&lArr; " + 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);