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

43
sapl/painel/consumers.py

@ -1,29 +1,42 @@
import json import json
import requests import requests
from asgiref.sync import async_to_sync
from channels.generic.websocket import AsyncJsonWebsocketConsumer from channels.generic.websocket import AsyncJsonWebsocketConsumer
from sapl.sessao.models import (ExpedienteMateria, OradorExpediente, OrdemDia,
PresencaOrdemDia, RegistroVotacao,
SessaoPlenaria, SessaoPlenariaPresenca,
VotoParlamentar, RegistroLeitura)
class PainelConsumer(AsyncJsonWebsocketConsumer): class PainelConsumer(AsyncJsonWebsocketConsumer):
async def connect (self): async def connect (self):
print('Conectado')
await self.accept() await self.accept()
async def disconnect(self, close_code): async def disconnect(self, close_code):
print('Disconectado:', close_code)
pass pass
async def receive(self, text_data): async def join_group(self):
print('Received Message') print('Group')
await self.channel_layer.group_add('painel', self.channel_name)
print('Enviando...')
url_dados = 'http://localhost:8000/painel/786/dados'
#response = requests.get(url_dados) async def receive(self, text_data):
#print(response) print('Received Message:' + text_data)
await self.send_json({ await self.send_data(786)
'message': 'teste'
}) async def send_data(self, id):
sessao = SessaoPlenaria.objects.get(id=id)
async def send(self, event):
print('Entrou') 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') r = session.get('http://localhost:8000/painel/786/dados')
json_data = r.json() 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