Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if message length exceeds 4096 bytes, it won't get processed correctly #46

Open
mewalig opened this issue Apr 7, 2016 · 4 comments
Open

Comments

@mewalig
Copy link

mewalig commented Apr 7, 2016

Here is a fix. It also performs better. Basically, it effectively does not increment the cache unless that data was actually processed into a MessageEvent.

  1. replace the cache variable with chars_processed
  2. replace the cache = '' line with chars_processed = 0
  3. replace the event handling code with:
          // process this.responseText
          var parts = responseText.substr(chars_processed).split("\n"),
              eventType = 'message',
              data = [],
              i = 0,
              line = '';

          // TODO handle 'event' (for buffer name), retry
          var temp_chars_processed = 0;
          for (; i < parts.length; i++) {
            line = parts[i].replace(reTrim, '');
            temp_chars_processed += line.length + (i + 1 == parts.length ? 0 : 1);
            if (line.startsWith('event')) {
              eventType = line.replace(/event:?\s*/, '');
            } else if (line.startsWith('retry')) {
              retry = parseInt(line.replace(/retry:?\s*/, ''));
              if(!isNaN(retry)) { interval = retry; }
            } else if (line.startsWith('data')) {
              data.push(line.replace(/data:?\s*/, ''));
            } else if (line.startsWith('id:')) {
              lastEventId = line.replace(/id:?\s*/, '');
            } else if (line.startsWith('id')) { // this resets the id
              lastEventId = null;
            } else if (line == '') {
              if (data.length) {
                var event = new MessageEvent(data.join('\n'), eventsource.url, lastEventId);
                eventsource.dispatchEvent(eventType, event);
                data = [];
                eventType = 'message';
                chars_processed += temp_chars_processed;
              }
            }
          }
@javabrett
Copy link

Thanks - do you have a pull-request for this?

@mewalig
Copy link
Author

mewalig commented Apr 7, 2016

No, sorry, I know almost nothing about github and don't know how to do that. If you want to point me to some steps I can learn without spending much time I'd be happy to try. Or I could send a diff or the updated EventSource.js file

@javabrett
Copy link

You can just go to https://github.com/remy/polyfills/blob/master/EventSource.js , click the pencil icon "Fork this project and edit the file". You can then save changes in your own fork, and GitHub should then prompt you to create a pull-request. Mention #46 in the commit-comment and pull request and it will automatically link here.

Thanks.

@mewalig
Copy link
Author

mewalig commented Apr 8, 2016

ok will do

mewalig added a commit to mewalig/polyfills that referenced this issue Apr 8, 2016
See remy#46. Update to properly and efficiently handle messages over 4096 bytes in size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants