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

Card events returned multiple times #34

Closed
mm-ns opened this issue Jan 21, 2018 · 1 comment
Closed

Card events returned multiple times #34

mm-ns opened this issue Jan 21, 2018 · 1 comment

Comments

@mm-ns
Copy link

mm-ns commented Jan 21, 2018

Hi eveyone,

I have a simple local app angularjs app and that sends a request to my backend (nodejs) to read an nfc tag and get a response back.

this is my method handling the request:

`
var sent = false;

	nfc = new NFC();
	nfc.on('reader', reader => {
		reader.on('card', async card => {
			try {
				const textRecord = ndef.Utils.createTextRecord(card.uid);
				const message = new ndef.Message([textRecord]);
				const bytes = message.toByteArray();
				const data = encapsulate(Buffer.from(bytes.buffer));
				await reader.write(4, data); // starts writing in block 4, continues to 5 and 6 in order to 
				
				if (sent) {
					console.log("sent : ", sent);
					return;
				} else {
					sent = true;
					socketServer.emit('nfcScanned', {
						result: "success",
						data: card
					});
					console.log("SENDING RESPONSE ", sent);
					return res.json({
						// result: "success"
						result: "success",
						data: card
					});
				}
			} catch (err) {
				// if (sent) {
				// 	console.log("ALREADY SENT");
				// } else {
				// nfc = undefined;
				// socketServer.emit('nfcNotScanned', {
				// 	result: "failed",
				// 	error: err
				// });

				// // sent = true;
				// return res.json({
				// 	// result: "success"
				// 	result: "error",
				// 	data: card
				// });
				// }
			}
		});

		reader.on('card.off', card => {
			
		});

		reader.on('error', err => {
		
		});

		reader.on('end', () => {
			
		});


	});

	nfc.on('error', err => {
		console.error(err);
	});`

What happens is when i trigger this method call the first time, all is good and i get the uid back on the client. However, each subsequent call the client makes to scan a new tag and get the next uid, it returns the result multiple times (first req, 1 response; 2nd request 2 responses, n requests - n responses).

Regardless of whether i manage to reduce the number of events i emit back to the client (socket triggers multiple messages although the regular http response is only triggering once), the reader gets slower and slower to read/write tag and it gets sort of exponentially slower. any idea?

@mm-ns
Copy link
Author

mm-ns commented Jan 22, 2018

Update: that was my bad :/ the modal i used to add some "user friendliness" was instantiating my controller again resulting in triggering multiple events.

@mm-ns mm-ns closed this as completed Jan 22, 2018
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

1 participant