Created
July 28, 2017 14:02
-
-
Save masterk63/2002ac59591b4dbea8724d70191f2370 to your computer and use it in GitHub Desktop.
Web Socket Inonic
This file contains hidden or 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
// 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