Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Gonzalez committed Jun 1, 2017
0 parents commit cee4243
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage/*
/libpeerconnection.log
npm-debug.log
testem.log
/typings

# e2e
/e2e/*.js
/e2e/*.map

#System Files
.DS_Store
Thumbs.db
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { SocketIoModule } from './src/socket-io.module';
export { SocketIoConfig } from './src/config/socket-io.config';
export { WrappedSocket } from './src/socket-io.service';
49 changes: 49 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "ngx-socket-io",
"version": "0.0.0",
"description": "Socket.IO module for Angular 4",
"main": "index.ts",
"scripts": {
"transpile": "ngc",
"package": "rollup -c",
"minify": "uglifyjs dist/bundles/socketio.umd.js --screw-ie8 --compress --mangle --comments --output dist/bundles/socketio.umd.min.js",
"build": "npm run transpile && npm run package && npm run minify"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/rodgc/ngx-socket-io.git"
},
"keywords": [
"Angular 4",
"Socket-io"
],
"author": "rodgc",
"license": "MIT",
"bugs": {
"url": "https://github.com/rodgc/ngx-socket-io/issues"
},
"homepage": "https://github.com/rodgc/ngx-socket-io#readme",
"dependencies": {
"@angular/core": "^4.1.3",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.10",
"rxjs": "^5.4.0",
"socket.io": "^2.0.1",
"zone.js": "^0.8.11"
},
"devDependencies": {
"@angular/cli": "^1.1.0",
"@angular/compiler": "^4.1.3",
"@angular/compiler-cli": "^4.1.3",
"@types/node": "^7.0.22",
"@types/socket.io": "^1.4.29",
"@types/socket.io-client": "^1.4.29",
"es6-shim": "^0.35.3",
"rollup": "^0.41.6",
"server-destroy": "^1.0.1",
"ts-node": "^3.0.4",
"tslint": "^5.4.0",
"typescript": "^2.3.4",
"uglify-js": "^3.0.14"
}
}
23 changes: 23 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default {
entry: 'dist/index.js',
dest: 'dist/bundles/socketio.umd.js',
sourceMap: false,
format: 'umd',
moduleName: 'ng.socketio',
globals: {
'@angular/core': 'ng.core',
'rxjs/Observable': 'Rx',
'rxjs/ReplaySubject': 'Rx',
'rxjs/add/operator/map': 'Rx.Observable.prototype',
'rxjs/add/operator/mergeMap': 'Rx.Observable.prototype',
'rxjs/add/observable/fromEvent': 'Rx.Observable',
'rxjs/add/observable/of': 'Rx.Observable',
'socket.io-client': 'io'
},
external:[
'@angular/core',
'rxjs/Observable',
'rxjs/add/operator/share',
'socket.io-client'
]
}
5 changes: 5 additions & 0 deletions src/config/socket-io.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** Config interface */
export interface SocketIoConfig {
url: string;
options?: any;
};
26 changes: 26 additions & 0 deletions src/socket-io.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ModuleWithProviders } from '@angular/core';
import { SocketIoConfig } from './config/socket-io.config';
import { WrappedSocket } from './socket-io.service';

/** Socket factory */
export function SocketFactory(config: SocketIoConfig) {
return new WrappedSocket(config);
}

export const socketConfig: string = "__SOCKET_IO_CONFIG__";

export class SocketIoModule {
static forRoot(config: SocketIoConfig): ModuleWithProviders {
return {
ngModule: SocketIoModule,
providers: [
{ provide: socketConfig, useValue: config },
{
provide: WrappedSocket,
useFactory: SocketFactory,
deps : [socketConfig]
}
]
};
}
}
63 changes: 63 additions & 0 deletions src/socket-io.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Injectable, EventEmitter } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/share';

import * as io from 'socket.io-client';

import { SocketIoConfig } from './config/socket-io.config';

@Injectable()
export class WrappedSocket {
subscribersCounter : number = 0;
ioSocket: any;

constructor(config: SocketIoConfig) {
const url: string = config.url || '';
const options: any = config.options || {};
var ioFunc = (io as any).default ? (io as any).default : io;
this.ioSocket = ioFunc(url, options);
}

on(eventName: string, callback: Function) {
this.ioSocket.on(eventName, callback);
}

once(eventName: string, callback: Function) {
this.ioSocket.once(eventName, callback);
}

connect() {
return this.ioSocket.connect();
}

disconnect(close?: any) {
return this.ioSocket.disconnect.apply(this.ioSocket, arguments);
}

emit(eventName: string, data: any, callback?: Function) {
return this.ioSocket.emit.apply(this.ioSocket, arguments);
}

removeListener(eventName: string, callback?: Function) {
return this.ioSocket.removeListener.apply(this.ioSocket, arguments);
}

removeAllListeners(eventName?: string) {
return this.ioSocket.removeAllListeners.apply(this.ioSocket, arguments);
}

/** create an Observable from an event */
fromEvent<T>(eventName: string): Observable<T> {
this.subscribersCounter++;
return Observable.create( (observer: any) => {
this.ioSocket.on(eventName, (data: T) => {
observer.next(data);
});
return () => {
if (this.subscribersCounter === 1)
this.ioSocket.removeListener(eventName);
};
}).share();
}

}
32 changes: 32 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"stripInternal": true,
"experimentalDecorators": true,
"strictNullChecks": true,
"noImplicitAny": true,
"module": "es2015",
"moduleResolution": "node",
"paths": {
"@angular/core": ["node_modules/@angular/core"],
"rxjs/*": ["node_modules/rxjs/*"]
},
"rootDir": ".",
"outDir": "dist",
"sourceMap": true,
"inlineSources": true,
"target": "es5",
"skipLibCheck": true,
"lib": [
"es2015",
"dom"
]
},
"files": [
"index.ts"
],
"angularCompilerOptions": {
"strictMetadataEmit": true
}
}

0 comments on commit cee4243

Please sign in to comment.