dunglas / mercure

🪽 An open, easy, fast, reliable and battery-efficient solution for real-time communications
https://mercure.rocks
GNU Affero General Public License v3.0
3.96k stars 294 forks source link

I will not change the domain name of cookie, how to solve it #36

Closed shijunti19 closed 5 years ago

shijunti19 commented 5 years ago

vue-cli3 vue My local address:http://localhost:8080/#/chat/index Server address:https://mercure.a031.com/ I use EventSource

let jwt='***jwt_key*';
const u = new URL(hubUrl)
    topicList.forEach(topic => u.searchParams.append('topic', topic))
    u.searchParams.append('Last-Event-ID', 2)
    var eventSourceInitDict = {
      withCredentials: false,
      headers: {
        Authorization: 'Bearer ' + jwt,
        Cookie: 'mercureAuthorization=' + jwt,
        'Access-Control-Allow-Origin': '*'
      }
    }
    eventSource = new EventSource(u, eventSourceInitDict)
    eventSource.onmessage = function(e) {
      console.log(e)
    }
    eventSource.onerror = error

image

Unauthorized

JWT_KEY='pasword' ADDR=':4000' CORS_ALLOWED_ORIGINS='*' LOG_FORMAT='json' DEMO=1  ./mercure

I will not change the domain name of cookie, how to solve it

shijunti19 commented 5 years ago

After studying for so long, I think EventSource is a very bad design. No wonder nobody uses it. Cook agent is a big problem and nginx agent can cause delays. There are too many things to set up.

dunglas commented 5 years ago

Did you looked at https://github.com/dunglas/mercure#401-unauthorized?

shijunti19 commented 5 years ago
vue.config.js
 devServer: {
    proxy: {
      '/api': {
        target: 'https://mercure.a031.com',
        secure: false,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '' //需要rewrite的
        }
      }
    }
  },

let hubUrl = 'https://mercure.a031.com/hub'
    if (process.env.NODE_ENV === 'development') {
      hubUrl = 'http://localhost:8080/api/hub'这行是重点,我弄了3天,必须是域名http://localhost:8080/api/不然 new URL无效的出错的。
  const u = new URL(hubUrl)
    }
shijunti19 commented 5 years ago

@dunglas How to empty the pushed data

Every time he refreshes, he reappears, not unsubscribe.

dunglas commented 5 years ago

You mean the history?

shijunti19 commented 5 years ago

@dunglas yes! image Yes, I delete the database clearance now.

dunglas commented 5 years ago

Indeed if you want to flush the history, you just have to delete the db file updates.db. But what's the benefit of doing this?

shijunti19 commented 5 years ago

@shijunti19 1.I have a lot of trouble developing and debugging. 2.When the page is refreshed,Chat is two dialogues. Now there's only a reply. He's meaningless. 3.I think we should add a setup to close the history. I just want the refresh to be cleared, and the history is loaded by myself through the database.

Does Android's own browser not support SSE now?

dunglas commented 5 years ago
  1. Can you explain exactly what issues you have so we can maybe improve the docs or the features of the hub?
  2. I'm not sure to understand what you mean. The demo chat doesn't have a history, you should indeed handle it at the app layer
  3. I'm not sure it will help, a better solution would be to (the internal db is only a kind of temporary persistence layer to retrieve missed events when EventSource reconnects after a network failure)
    • send the POST request to the Python app
    • store the message in an external DB
    • dispatch the update from the Python app to the Mercure Hub
    • when a new client connects to the chat, fetch the history from the external DB
  4. Yes Android Browser does support SSE: https://caniuse.com/#feat=eventsource