Skip to content

Instantly share code, notes, and snippets.

@danpadua
Created May 6, 2015 12:25
Show Gist options
  • Save danpadua/0d18f314f74dc5a973c0 to your computer and use it in GitHub Desktop.
Save danpadua/0d18f314f74dc5a973c0 to your computer and use it in GitHub Desktop.
I'm not using chat for facebook . The problem is that I can not receive messages , I can only send them .
# My code dont work in this part:
# $this.connection.rawInput = rawInput;
# $this.connection.rawOutput = rawOutput;
#
# Error:
# Uncaught TypeError: object is not a function
Webapp.ChatView = Ember.View.extend
boshService: 'http://im.domain.com:5280/'
connection: null
action:->
@sendMsg()
onConnect:(status)->
$this = @
if status == Strophe.Status.CONNECTING
console.log( Webapp.user.keyUser + ' está conectando ao chat.')
else if status == Strophe.Status.CONNFAIL
console.log(Webapp.user.keyUser + ' falha na conexão com o chat.')
Webapp.alert("Falha na conexão.")
else if status == Strophe.Status.DISCONNECTING
console.log(Webapp.user.keyUser + ' está desconectando.')
history.back()
Webapp.alert("Você foi desconectado do chat", -> bootbox.hideAll())
else if status == Strophe.Status.DISCONNECTED
console.log(Webapp.user.keyUser +' foi desconectado do chat.')
else if status == Strophe.Status.CONNECTED
console.log(Webapp.user.keyUser + ' está conectado ao chat.')
$this.connection.addHandler($this.onMessage, null, 'message', null, null, null)
$this.connection.addHandler($this.onOwnMessage, null, 'iq', 'set', null, null);
$this.connection.send($pres().tree())
onMessage:(msg)->
to = msg.getAttribute('to')
from = msg.getAttribute('from')
type = msg.getAttribute('type')
elems = msg.getElementsByTagName('body')
if type == "chat" && elems.length > 0
body = elems[0]
console.log('BOT_MESSAGE DIZ: Eu recebi uma mensagem de ' + from + ': ' + Strophe.getText(body))
reply = $msg({to: from, from: to, type: 'chat'}).cnode(Strophe.copyElement(body))
$this.connection.send(reply.tree())
console.log('BOT_MESSAGE DIZ: Eu enviei de ' + from + ': ' + Strophe.getText(body))
return true
onOwnMessage:(msg) ->
$this = @
console.log(msg)
elems = msg.getElementsByTagName('own-message')
if elems.length > 0
own = elems[0]
to = msg.getAttribute('to')
from = msg.getAttribute('from')
iq = $iq({
to: from,
type: 'error',
id: msg.getAttribute('id')
}).cnode(own).up().c('error', {type: 'cancel', code: '501'}).c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'})
$this.connection.sendIQ(iq)
return true
sendMessage:(msg) ->
$this = @
message = msg
to = "825999@domain.com" #Trocar para o taxista que atendeu a chamada
if message && to
reply = $msg({
to: to,
type: 'chat'
}).cnode(Strophe.xmlElement('body', message)).up().c('active', {xmlns: "http://jabber.org/protocol/chatstates"})
$this.connection.send(reply)
console.log('Enviado para, ' + to + ': ' + message)
didInsertElement:->
$this = @
id = Webapp.user.keyUser + '@domain.com'
pass = Webapp.user.hashUser
if $this.connection is null
$this.connection = new Strophe.Connection($this.boshService);
$this.connection.rawInput = $this.rawInput();
$this.connection.rawOutput = $this.rawOutput();
$this.connection.connect(id, pass, $this.onConnect)
$('#sendMsg').keypress((event)->
value = document.getElementById('sendMsg').value
key = event.which
if key is 13
if value isnt ""
$this.sendMessage(value)
document.getElementById('sendMsg').value = ""
$this.log(value)
)
sendMsg:->
$this = @
value = document.getElementById('sendMsg').value
if value isnt ""
$this.sendMessage(value)
document.getElementById('sendMsg').value = ""
$this.log(value)
log:(msg)->
$('#msg').append('<div></div>').append(document.createTextNode(msg))
rawInput:(data)->
$this = @
$this.log(data)
rawOutput:(data)->
$this = @
$this.log('CAB: ' + data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment