Skip to content

Instantly share code, notes, and snippets.

@ingeit
Created July 28, 2017 14:19
Show Gist options
  • Save ingeit/69ae626894662d145336cce59c097d23 to your computer and use it in GitHub Desktop.
Save ingeit/69ae626894662d145336cce59c097d23 to your computer and use it in GitHub Desktop.
Web Socket Inonic
// npm install socket.io-client --save
import * as io from 'socket.io-client';
socket:any
chat_input:string;
chats = [];
constructor(public navCtrl: NavController) {
this.socket = io('http://localhost:3000');
this.socket.on('message', (msg) => {
console.log("message", msg);
this.chats.push(msg);
});
}
send(msg) {
if(msg != ''){
this.socket.emit('message', msg);
}
this.chat_input = '';
}
// ejemplo HTML
/*
<ion-content padding>
<ion-list>
<ion-item *ngFor="let message of chats">{{message}}</ion-item>
</ion-list>
<ion-item>
<ion-input type="text" [(ngModel)]="chat_input" placeholder="Enter message"></ion-input>
</ion-item>
<button ion-button block (click)="send(chat_input)">Send</button>
</ion-content>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment