Skip to content

Commit 4a7f953

Browse files
author
sitegui
committed
node package published
1 parent 241e7e1 commit 4a7f953

8 files changed

+34
-49
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*~

Connection.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
/*
2-
Represents a connection (both client and server sides)
3-
*/
4-
5-
var util = require("util")
6-
var events = require("events")
7-
var crypto = require("crypto")
8-
var InStream = require("./InStream.js")
9-
var OutStream = require("./OutStream.js")
10-
var frame = require("./frame.js")
11-
var Server = require("./Server.js")
1+
// Represents a connection (both client and server sides)
122

133
// socket is a net or tls socket
144
// parent can be a Server or, in case of client connection, a string with the path to connect to
@@ -57,6 +47,15 @@ function Connection(socket, parent, callback) {
5747
this.on("connect", callback)
5848
}
5949

50+
module.exports = Connection
51+
var util = require("util")
52+
var events = require("events")
53+
var crypto = require("crypto")
54+
var InStream = require("./InStream.js")
55+
var OutStream = require("./OutStream.js")
56+
var frame = require("./frame.js")
57+
var Server = require("./Server.js")
58+
6059
// Minimum size of a pack of binary data to send in a single frame
6160
Connection.binaryFragmentation = 512*1024 // .5 MiB
6261

@@ -391,5 +390,3 @@ Connection.prototype.processCloseFrame = function (payload) {
391390
this.readyState = this.CLOSED
392391
this.emit("close", code, reason)
393392
}
394-
395-
module.exports = Connection

InStream.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
/*
2-
Simple wrapper for stream.Readable, used for receiving binary data
3-
*/
4-
5-
var util = require("util")
6-
var stream = require("stream")
1+
// Simple wrapper for stream.Readable, used for receiving binary data
72

83
// Represents the readable stream for binary frames
94
// Events: readable(), end()
105
function InStream() {
116
stream.Readable.call(this)
127
}
138

9+
module.exports = InStream
10+
var util = require("util")
11+
var stream = require("stream")
12+
1413
// Extends the basic readable stream and implement _read
1514
util.inherits(InStream, stream.Readable)
1615
InStream.prototype._read = function (size) {
@@ -25,5 +24,3 @@ InStream.prototype.addData = function (data) {
2524
InStream.prototype.end = function () {
2625
this.push(null)
2726
}
28-
29-
module.exports = InStream

OutStream.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
/*
2-
Simple wrapper for stream.Writable, used for sending binary data
3-
*/
4-
5-
var util = require("util")
6-
var stream = require("stream")
7-
var frame = require("./frame.js")
1+
// Simple wrapper for stream.Writable, used for sending binary data
82

93
// Represents the writable stream for binary frames
104
// Events:
@@ -23,6 +17,11 @@ function OutStream(connection, minSize) {
2317
})
2418
}
2519

20+
module.exports = OutStream
21+
var util = require("util")
22+
var stream = require("stream")
23+
var frame = require("./frame.js")
24+
2625
// Extends the basic writable stream and implements _write
2726
util.inherits(OutStream, stream.Writable)
2827
OutStream.prototype._write = function (chunk, encoding, callback) {
@@ -41,5 +40,3 @@ OutStream.prototype._write = function (chunk, encoding, callback) {
4140
} else
4241
callback()
4342
}
44-
45-
module.exports = OutStream

Server.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
/*
2-
Represents a websocket server
3-
*/
4-
5-
var util = require("util")
6-
var net = require("net")
7-
var tls = require("tls")
8-
var Connection = require("./Connection.js")
9-
var events = require("events")
1+
// Represents a websocket server
102

113
// new Server() creates a new ws server and starts listening for new connections
124
// Events: listening(), close(), error(err), connection(conn)
@@ -52,6 +44,13 @@ function Server(secure, options, callback) {
5244
this.on("connection", callback)
5345
}
5446

47+
module.exports = Server
48+
var util = require("util")
49+
var net = require("net")
50+
var tls = require("tls")
51+
var Connection = require("./Connection.js")
52+
var events = require("events")
53+
5554
// Makes Server also an EventEmitter
5655
util.inherits(Server, events.EventEmitter)
5756

@@ -74,5 +73,3 @@ Server.prototype.listen = function (port, host, callback) {
7473

7574
return this
7675
}
77-
78-
module.exports = Server

frame.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*
2-
Utility functions for creating frames
3-
*/
1+
// Utility functions for creating frames
42

53
// Creates a text frame
64
// data is a string

index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
3-
*/
4-
51
var Server = require("./Server.js")
62
var Connection = require("./Connection.js")
73
var net = require("net")
@@ -12,6 +8,8 @@ var url = require("url")
128
// options is an object to be passed to net.createServer() or tls.createServer(), with the additional property "secure" (a boolean)
139
// callback will be added as "connection" listener
1410
exports.createServer = function (options, callback) {
11+
if (typeof options == "function")
12+
return new Server(false, options)
1513
return new Server(Boolean(options.secure), options, callback)
1614
}
1715

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-websocket",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"author": "Sitegui <[email protected]>",
55
"description": "Basic server&client approach to websocket (text and binary frames)",
66
"main": "./index.js",

0 commit comments

Comments
 (0)