Skip to content

Instantly share code, notes, and snippets.

@robophil
Created February 6, 2017 14:08
Show Gist options
  • Save robophil/84797fa463501c677f3ec1686d2527e7 to your computer and use it in GitHub Desktop.
Save robophil/84797fa463501c677f3ec1686d2527e7 to your computer and use it in GitHub Desktop.
sample connection to sails socket from angular2
import { Injectable, EventEmitter } from '@angular/core'
import { Subject } from 'rxjs/Subject'
import { Donor } from './donor.interface'
import * as socketIO from 'socket.io-client'
import * as sailsIO from 'sails.io'
const url = 'http://localhost:1337'
@Injectable()
export class SocketService {
private donorInit = new Subject<Object>()
private donorUpdate = new Subject<Object>()
private io: any
donorInit$ = this.donorInit.asObservable()
donorUpdate$ = this.donorUpdate.asObservable()
constructor() {
this.init()
}
init() {
const that = this
that.io = sailsIO(socketIO)
that.io.sails.reconnection = true
that.io.sails.url = url
that.io.socket.on('connect', function () {
that.start()
console.info("connected to socket server")
})
that.io.socket.on('disconnect', function () {
console.error("disconnected from socket server")
})
}
private start() {
const that = this
that.io.socket.get('/donor', function (data, jwres) {
that.announceInitData(data)
})
that.io.socket.on('donor', function (data) {
console.log(data)
that.announceNewData(data)
})
}
public registerDonor(donor): Promise<Donor> {
const that = this
return new Promise((resolve, reject) => {
return that.io.socket.post('/donor', donor, function (data, jwres) {
if (jwres.statusCode >= 200 && jwres.statusCode < 400) {
that.announceNewData({
id: data.id,
verb: 'created',
data: data
})
return resolve(data)
}
else return reject({ error: "invalid response", msg: data })
})
})
}
public getDonor(id): Promise<Donor> {
const that = this
return new Promise((resolve, reject) => {
return that.io.socket.get('/donor/' +id, function (data, jwres) {
console.log(data)
console.log(id)
if (jwres.statusCode >= 200 && jwres.statusCode < 400) {
return resolve(data)
}
else return reject({ error: "invalid response", msg: data })
})
})
}
public updateDonor(donor): Promise<Donor> {
const that = this
return new Promise((resolve, reject) => {
return that.io.socket.put('/donor/' + donor.id, donor, function (data, jwres) {
if (jwres.statusCode >= 200 && jwres.statusCode < 400) {
that.announceNewData({
id: data.id,
verb: 'updated',
data: data
})
return resolve(data)
}
else return reject({ error: "invalid response", msg: data })
})
})
}
public deleteDonor(donor): Promise<Donor> {
const that = this
return new Promise((resolve, reject) => {
return that.io.socket.delete('/donor/' + donor.id, function (data, jwres) {
if (jwres.statusCode >= 200 && jwres.statusCode < 400) {
that.announceNewData({
id: data.id,
verb: 'destroyed',
previous: data
})
return resolve(data)
}
else return reject({ error: "invalid response", msg: data })
})
})
}
private announceInitData(data) {
this.donorInit.next(data)
}
private announceNewData(data) {
this.donorUpdate.next(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment