I am trying to use php ratchet library (not node server script) as my socket server, however when I try to connect to it, I get cross origin request issue. Here is my ratchet server code:
<?php
require 'vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use ChatApp\Chat;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
3000
);
$server->run();
Here is my code to connect to it via ionic app:
import { Component, NgZone, ViewChild } from '@angular/core';
import { NavController, Content } from 'ionic-angular';
import * as io from 'socket.io-client';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild(Content) content: Content;
messages: any = [];
socketHost: string = "http://localhost:3000";
socket: any;
chat: any;
username: string;
zone: any;
constructor(public navCtrl: NavController) {
this.socket = io.connect(this.socketHost);
this.zone = new NgZone({ enableLongStackTrace: false });
this.socket.on("chat message", (msg) => {
this.zone.run(() => {
this.messages.push(msg);
this.content.scrollToBottom();
});
});
}
chatSend(v) {
let data = {
message: v.chatText,
username: this.username
}
this.socket.emit('new message', data);
this.chat = '';
}
}
Can anyone please help?
Thanks