Browse Source

Reccepção de dados no VueJS

Websocket_painel
AlGouvea 3 years ago
parent
commit
4069b94f6b
  1. 12
      frontend/src/__apps/painel/main.js
  2. 43
      sapl/painel/consumers.py
  3. 2
      sapl/painel/tasks.py

12
frontend/src/__apps/painel/main.js

@ -250,14 +250,14 @@ const v = new Vue({ // eslint-disable-line
}, 100)
},
foi: function foi () {
socket.send('teste')
socket.send('Teste de entrada')
},
pollData () {
this.fetchData()
this.polling = setInterval(() => {
// console.info('Fetching data from backend')
this.fetchData()
}, 100)
this.foi()
}, 30000)
}
},
beforeDestroy () {
@ -269,11 +269,9 @@ const v = new Vue({ // eslint-disable-line
console.log('Connection established')
}
const _this = this
socket.onmessage = function (e) {
_this.teste = e.data
console.log(_this.teste)
this.teste = JSON.parse(e.data)
console.log(this.teste.sessao_plenaria)
}
socket.onclose = function (e) {

43
sapl/painel/consumers.py

@ -1,29 +1,42 @@
import json
import requests
from asgiref.sync import async_to_sync
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from sapl.sessao.models import (ExpedienteMateria, OradorExpediente, OrdemDia,
PresencaOrdemDia, RegistroVotacao,
SessaoPlenaria, SessaoPlenariaPresenca,
VotoParlamentar, RegistroLeitura)
class PainelConsumer(AsyncJsonWebsocketConsumer):
async def connect (self):
print('Conectado')
await self.accept()
async def disconnect(self, close_code):
print('Disconectado:', close_code)
pass
async def receive(self, text_data):
print('Received Message')
print('Enviando...')
url_dados = 'http://localhost:8000/painel/786/dados'
async def join_group(self):
print('Group')
await self.channel_layer.group_add('painel', self.channel_name)
#response = requests.get(url_dados)
#print(response)
await self.send_json({
'message': 'teste'
})
async def send(self, event):
print('Entrou')
async def receive(self, text_data):
print('Received Message:' + text_data)
await self.send_data(786)
async def send_data(self, id):
sessao = SessaoPlenaria.objects.get(id=id)
response = {
'sessao_plenaria': str(sessao),
'sessao_plenaria_data': sessao.data_inicio.strftime('%d/%m/%Y'),
'sessao_plenaria_hora_inicio': sessao.hora_inicio,
'sessao_solene': sessao.tipo.nome == "Solene",
'sessao_finalizada': sessao.finalizada,
'tema_solene': sessao.tema_solene,
'status_painel': sessao.painel_aberto,
}
await self.send_json(response)

2
sapl/painel/tasks.py

@ -24,4 +24,4 @@ def get_cronometro():
r = session.get('http://localhost:8000/painel/786/dados')
json_data = r.json()
async_to_sync(channel_layer.group_send)('message', {'type':'send_message', 'text': json_data})
async_to_sync(channel_layer.group_send)('painel', {'type':'send_data', 'message': json_data})

Loading…
Cancel
Save