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

Unbind listeners on serverside with close method #110

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ That's all you need to get started. For the detailed API, continue reading belo
- [SocketIOFileUpload.listen(app)](#socketiofileuploadlistenapp)
- [SocketIOFileUpload.router](#socketiofileuploadrouter)
- [instance.listen(socket)](#instancelistensocket)
- [instance.close(socket)](#instanceclosesocket)
- [instance.abort(id, socket)](#instanceabortid-socket)
- [instance.dir = "/path/to/upload/directory"](#instancedir--pathtouploaddirectory)
- [instance.mode = "0666"](#instancemode--0666)
Expand Down Expand Up @@ -438,7 +439,7 @@ instance.wrapData = {
#### instance.exposePrivateFunction = false

If true this will expose some functions used in intern to personalize action on the topic. This is used alongside with wrapData to add custom check or logic before process the file upload.
If true you will have access to:
If true you will have access to:
```
instance.chunckCallback
instance.readyCallback
Expand Down Expand Up @@ -557,6 +558,18 @@ io.sockets.on("connection", function(socket){
});
```

#### instance.close(socket)

No longer listen for uploads occuring on this Socket.IO socket.

```javascript
io.sockets.on("connection", function(socket){
var uploader = new SocketIOFileUpload();
uploader.listen(socket);
uploader.close(socket);
});
```

#### instance.abort(id, socket)

Aborts an upload that is in progress. Example use case:
Expand Down
34 changes: 33 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ function SocketIOFileUploadServer(options) {
*/
self.topicName = _getOption("topicName", "siofu");

/**
* Object for keeping the references to bound functions
* This is neccessary because the disconnect listener key
* is not unique.
*/
self.functions = {
// "start" : {},
// "progress" : {},
// "complete" : {},
"disconnect" : {}
};


/**
* WrapData allow you to wrap the Siofu messages into a predefined format.
* You can then easily use Siofu packages even in strongly typed topic.
Expand Down Expand Up @@ -602,10 +615,29 @@ function SocketIOFileUploadServer(options) {
socket.on(self.topicName + "_progress", _uploadProgress(socket));
socket.on(self.topicName + "_done", _uploadDone(socket));
}
self.functions.disconnect[socket.id] = _onDisconnect(socket);
socket.on("disconnect", self.functions.disconnect[socket.id]);
};

socket.on("disconnect", _onDisconnect(socket));
/**
* Public method. Undo listening from listen method.
*
* @param {Socket} socket The socket on which not to listen anymore
* @return {void}
*/
this.close = function (socket) {
sffc marked this conversation as resolved.
Show resolved Hide resolved
if(_isWrapDataWellConfigured() && self.wrapData)
socket.removeAllListeners(self.topicName);
else {
socket.removeAllListeners(self.topicName + "_start");
socket.removeAllListeners(self.topicName + "_progress");
socket.removeAllListeners(self.topicName + "_done");
}

socket.off("disconnect", self.functions.disconnect[socket.id]);
};


/**
* Public method. Abort an upload that may be in progress. Throws an
* exception if the specified file upload is not in progress.
Expand Down