Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Channel = {"#chat": channel}
- channel.eventBuffer[] = {event, text}
- channel.users
- channel.topic
- */
- var Channel = Class.create({
- users: [],
- topic: "",
- buffer: [],
- initialize: function() {
- self = this;
- },
- })
- var Client = Class.create({
- self: null,
- socket: null,
- buffer: "",
- nickname: null,
- initialize: function() {
- self = this;
- },
- connect: function(host, nickname) {
- this.nickname = nickname
- this.socket = new WebSocket(host);
- this.socket.type =
- this.socket.onopen = this.onOpen;
- this.socket.onclose = this.onClose;
- this.socket.onmessage = this.onMessage;
- this.socket.onerror = this.onError;
- },
- disconnect: function() {
- console.log(e)
- },
- onError: function(e) {
- console.log("error", e)
- },
- onMessage: function(e) {
- var lines = (self.buffer + e.data).split("\r\n");
- self.buffer = lines.pop();
- lines.each(function(line) {
- var prefix="", args=[];
- if (!line.startsWith(":")) {
- return;
- }
- if (line[0] == ':') {
- var temp = line.split(" ");
- prefix = temp[0].substr(1);
- temp.shift();
- line = temp.join(" ")
- }
- if (line.search(" :") != -1) {
- var temp = line.split(" :");
- var trailing = temp[1];
- args = temp[0].split(" ")
- args.push(trailing);
- } else {
- args = line.split();
- }
- var command = args[0];
- args.shift();
- self.onCommandMessage(prefix, command, args);
- });
- },
- onOpen: function(e) {
- self.sendRaw("NICK " + self.nickname);
- self.sendRaw("USER " + self.nickname + " " + self.nickname + " " + self.nickname + " " + self.nickname);
- },
- onClose: function(e) {
- console.log("close", e)
- },
- send: function(command, args, trail) {
- this.sendRaw(line)
- },
- sendRaw: function(line) {
- this.socket.send(line + "\r\n")
- },
- onCommandMessage: function(sender, command, arguments) {
- switch(command) {
- case "001":
- this.ircWelcome();
- break
- case "JOIN":
- this.doJoin();
- break;
- case "PART":
- this.doPart();
- break
- case "PRIVMSG":
- this.doPrivmsg();
- break
- case "NOTICE":
- this.doNotice();
- break
- case "QUIT":
- this.doQuit();
- break
- case "NICK":
- this.doNick();
- break
- default:
- console.log("Discarding ", sender, command, arguments)
- }
- },
- ircWelcome: function() {
- console.log("welcome");
- },
- ircJoin: function(user, channel) {
- console.log(user, channel);
- },
- ircPart: function(user, channel, reason) {
- console.log(user, channel, reason);
- },
- ircQuit: function(user, reason) {
- console.log(user, channel);
- },
- ircNick: function(user, oldnick, newnick) {
- console.log(user, oldnick, newnick);
- },
- ircPrivmsg: function(user, target, message) {
- console.log(user, target, message);
- },
- ircNotice: function(user, target, message) {
- console.log(user, channel, message);
- },
- });
- var client = new Client();
- client.connect("ws://chat.kevelbreh.co.za:8889", "websocket-user")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement