Last active
June 13, 2018 08:52
-
-
Save EnricoPicci/35f3c3a2a2a3f96cfdf7b89d46a5d499 to your computer and use it in GitHub Desktop.
Sockets as Observable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Server } from 'http'; | |
import { Observable } from 'rxjs'; | |
import { Observer } from 'rxjs'; | |
import * as socketIoServer from 'socket.io'; | |
import {SocketObs} from './socket-obs'; | |
export function sockets(httpServer: Server, port) { | |
httpServer.listen(port, () => { | |
console.log('Running server on port %s', port); | |
}); | |
return new Observable<SocketObs>( | |
(subscriber: Observer<SocketObs>) => { | |
socketIoServer(httpServer).on('connect', | |
socket => { | |
console.log('client connected'); | |
subscriber.next(new SocketObs(socket)); | |
} | |
); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment