This is the first time I am using sockets and started off with Ratchet but I seriously cant fit my head into it. Still tried to join together some parts from the tutorial but facing some problems. Plus I would like to know how to use autobahn.js with it. The tutorials are not clear.
My Problem
1) How to send a message to all users except the current user stating "
... joined" and the '...' has to be the ip of the user.
I tried the following but it gives me error on the terminal.
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
$this->send('new');
echo "New connection! ({$conn->remoteAddress})\n";
}
Call to undefined method MyApp\Chat::send()
2) What to do so that when a message is sent all the parties can view it including the one who is sending it (that's the way how every chat works)?
JS
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
//console.log("Connection established!");
$("#chat_window #messages_list").append("<li class='join_disconnect'>127.0.0.1 Online</li>");
}
conn.onmessage = function(e) {
//console.log(e.data);
$("#chat_window #messages_list").append("<li class='thread'>"+e.data+"</li>");
}
conn.onclose = function(e) {
//console.log("Connection Disconnected!");
$("#chat_window #messages_list").append("<li class='join_disconnect'>127.0.0.1 Offline</li>");
};
$(document).ready(function(){
disable_button();
//EMERGENCY EXIT
$('#exit').click(function(){
window.location.replace('http://www.google.com');
});
//PREVENT BLANK INPUT
$('#msg').on('keyup', function(e){
if($(this).val().length>0){
enable_button();
}
else{
disable_button();
}
});
//SEND MESSAGE
$('#snd').click(function(){
var thread = $('#msg').val();
//console.log(thread);
//conn.send(thread);
$.ajax({
type:'POST',
url: './bin/send-message.php',
data: {msg: thread},
success: function(response){
//alert(response);
if(response!=1){
$('#msg').val('');
disable_button();
}
}
});
});
//ENABLE BUTTON
function enable_button() {
var element = document.getElementById('snd');
$(element).addClass('active');
element.style.pointerEvents = '';
}
//DISABLE BUTTON
function disable_button() {
var element = document.getElementById('snd');
$(element).removeClass('active');
element.style.pointerEvents = 'none';
}
});
I know these are a lot of questions but I really want to know how. If there are any step by step easy to learn tutorials those are also welcome.
If you are trying to change example from the tutorial at the Rachet site, than for your first problem solution is:
public function onOpen(ConnectionInterface $conn) {
// first, you are sending to all existing users message of 'new'
foreach ($this->clients as $client) {
$client->send('new');
}
// than,
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
Regarding your second question, if I get you right, you should send new messages to all connected clients, like this:
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $client) {
// remove this check
// if ($from !== $client) {
// The sender is not the receiver, send to each client connected
// $client->send($msg);
// }
// just send to all
$client->send($msg);
}
}
Update: complete solution.
In the Chat.php, you need to modify some methods:
public function onOpen(ConnectionInterface $conn) {
// first, you are sending to all existing users message of 'new'
foreach ($this->clients as $client) {
$client->send('<status>' . $conn->remoteAddress . ' Online</status>'); //here we are sending a status-message
}
// than,
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->remoteAddress})\n";
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);
//send to others clients message about disconnected user
foreach ($this->clients as $client) {
$client->send('<status>' . $conn->remoteAddress . ' Offline</status>'); //here we are sending a status-message too
}
echo "Connection {$conn->remoteAddress} has disconnected\n";
}
in your js code, modify next method:
conn.onmessage = function(e) {
//console.log(e.data);
var match = e.data.match(/^<status>(.*?)<\/status>$/i);
if (match) {
if (/\d+\.\d+\.\d+\.\d+ online/i.test(match[1])) {
messages.append('<li class="join_connect">' + match[1] + "</li>");
} else if (/\d+\.\d+\.\d+\.\d+ offline/i.test(match[1])) {
messages.append('<li class="join_disconnect">' + match[1] + "</li>");
}
} else {
messages.append('<li class="thread">' + e.data + "</li>");
}
};
Related
I'm new to WebRTC and I don't understand how the signaling works on the server side. I know the request must be two-way. I'm able to send an offer to a PHP page but from there I'm stuck.. How does the other peer pick that offer so that it can generate an answer? I don't want a socket solution because I don't have my own server and I don't want to have a third-party dependency.
I have created an offer and sent that offer to a PHP page. See my code:
var myPeerConnection;
var caller_video=document.getElementById('caller_video');
var receiver_video=document.getElementById('receiver_video');
var mediaConstraints = {
video:true,
audio:false
};
function sendToServer(msg){
var msgJSON = JSON.stringify(msg);
$.ajax({
type:'get',
url:'offer.php',
data:'object='+msgJSON,
beforeSend:function(){
console.log('Sending...');
},
success:function(data){
console.log(data);
}
});
}
function reportError(error){
console.log(error.name);
}
function handleNegotiationNeededEvent(){
myPeerConnection.createOffer().then(function(offer) {
return myPeerConnection.setLocalDescription(offer);
})
.then(function(){ // so here I'm supposed to send an offer
sendToServer({
name: myUsername,
target: targetUsername,
type: "video-offer",
sdp: myPeerConnection.localDescription
});
})
.catch(reportError);
}
function handleICECandidateEvent(event){
if(event.candidate){//send the ICECandidates
sendToServer({
name: myUsername,
target: targetUsername,
type: "new-ice-candidate",
candidate: event.candidate
});
}
}
function handleTrackEvent(event){
console.log(event);
document.getElementById("received_video").srcObject = event.streams[0];
}
function handleRemoveTrackEvent(event){
var stream = document.getElementById("received_video").srcObject;
var trackList = stream.getTracks();
if (trackList.length == 0){
closeVideoCall();
}
}
function handleICEConnectionStateChangeEvent(event){
console.log('ICE connection changed!');
switch(myPeerConnection.iceConnectionState) {
case "closed":
case "failed":
case "disconnected":
closeVideoCall();
break;
}
}
function handleICEGatheringStateChangeEvent(event){
console.log('Is gathering');
console.log(event);
}
function handleSignalingStateChangeEvent(event) {
console.log('Signaling state changed');
switch(myPeerConnection.signalingState) {
case "closed":
closeVideoCall();
break;
}
};
function createPeerConnection() {
var STUN = {
'url': 'stun:stun.l.google.com:19302',
};
var iceServers =
{
iceServers: [STUN]
};
myPeerConnection = new RTCPeerConnection(iceServers);
myPeerConnection.onnegotiationneeded = handleNegotiationNeededEvent;
myPeerConnection.onicecandidate = handleICECandidateEvent;
myPeerConnection.ontrack = handleTrackEvent;
myPeerConnection.onremovetrack = handleRemoveTrackEvent;
myPeerConnection.oniceconnectionstatechange = handleICEConnectionStateChangeEvent;
myPeerConnection.onicegatheringstatechange = handleICEGatheringStateChangeEvent;
myPeerConnection.onsignalingstatechange = handleSignalingStateChangeEvent;
}
function handleGetUserMediaError(e) {
switch(e.name) {
case "NotFoundError":
alert("Unable to open your call because no camera and/or microphone" +
"were found.");
break;
case "SecurityError":
case "PermissionDeniedError":
// Do nothing; this is the same as the user canceling the call.
break;
default:
alert("Error opening your camera and/or microphone: " + e.message);
break;
}
closeVideoCall();
}
function closeVideoCall(){
//do something to exit the video call
}
//invite the other peer...we want to send our SDP
function invite(evt){
if (myPeerConnection){
console.log('Call already started');
}
else{
//myPeerConnection=new MediaStream();
targetUsername ='Nevil';//Unique other peer username
myUsername='Philip';
createPeerConnection();//this function creates a peer connection//uses the STUN/TURNS servers...updates myPeerConnection variable so it's not null
navigator.mediaDevices.getUserMedia(mediaConstraints)//grab our media constraints
.then(function(localStream) {
caller_video.srcObject =localStream;
caller_video.play();
localStream.getTracks().forEach(track => myPeerConnection.addTrack(track, localStream));
})
.catch(handleGetUserMediaError);
}
}
//we click the call button
document.querySelector('#callBt').addEventListener('click',function(){
invite();
});
I know there must be a way to send back the answer but I would like to just send the offer; that way I will understand how both peers exchange the SDP on the backend. Please try using PHP. If anyone can create a signaling XHR request, I will appreciate it.
I'm trying to implement google sign-in to on my website.
I've done the steps from here Authenticate with Google.
This function executes after i have logged in to google :
function onSignIn(googleUser) {
var googleResponse = googleUser.getAuthResponse();
google_login(googleResponse, true);
};
Google_login function:
function google_login(res) {
var httpObject = getXMLHTTPObject();
var ajax_url = siteURL + 'google_login';
var params = 'token='+encodeURIComponent(res.id_token);
httpObject.open('POST', ajax_url, true);
httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpObject.onreadystatechange = function() {
if (httpObject.readyState == 4) {
if(httpObject.responseText == 'true') {
window.location = httpObject.responseURL;
}
else {
if(httpObject.responseText == '') {
window.location = siteURL + 'login_again';
}
else {
window.location = siteURL + 'google_login_error';
}
}
}
};
httpObject.send(params);
}
And in my model I'm using this code:
private $google_client;
function Google_model() {
parent::__construct();
$this->google_client = new Google_Client(['client_id' => 'my_client_id','client_secret' =>'my_client_secret']);
}
function check_google_user($access_token) {
$payload = $this->google_client->verifyIdToken($access_token);
if ($payload) {
return $payload;
}
return false;
}
In my controller I'm calling check_google_user function.
And here appears a strange behaviour. Sometimes when I try to login I get the payload, and sometimes not (PS: I'm trying to login with the same user in the same day). Am I doing something wrong?
EDIT:
I'm getting this error: Caught exception: Cannot handle token prior to 2017-01-25T16:20:24+0200
Solved this by commenting these lines in firebase JWT.php file:
throw new BeforeValidException(
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
);
I'm using davibennun/laravel-push-notification for sending notification.
When I call the notification method and return $push->getFeedback(); from my api, I get <html></html> tag in response body and don't get any notification on my device.
Here is my api method:
public function sendnotif(Request $request)
{
$push = PushNotification::app('appNameIOS')
->to($request['deviceToken'])
->send('Hello World, i`m a push message');
return $push->getFeedback();
}
and in ionic app.js file:
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
if(ionic.Platform.isIOS()){
console.log('ios');
if (notification.alert) {
navigator.notification.alert(notification.alert);
}
if (notification.sound) {
var snd = new Media(event.sound);
snd.play();
}
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge).then(function(result) {
console.log('badgeResult');
console.log(result);
}, function(err) {
console.log('badgeResult');
console.log(err);
});
}
}
else if(ionic.Platform.isAndroid()){
console.log('android');
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
else{
console.log('can not detect device');
}
});
After hours searching, I finally found the solution.
Now in my api I return result of notification with getAdapter()->getResponse() with this lines of code:
foreach ($push->pushManager as $push) {
$response = $push->getAdapter()->getResponse();
}
return var_dump($response);
This code returns you array of device tokens that not valid so if your result array is empty, that means all notifications was sent.
Also on my ionic app I had some js error. After fix them, I get notifications successfully on device.
This question already has answers here:
Redirect PHP contact form to another page upon success [closed]
(3 answers)
Closed 8 years ago.
How can I redirect to another page in PHP upon successful form submission?
Right now I have the code that shows a Green Message under Submit button: Your message was successfully submitted!
I'd like to redirect to success.php and in case of an error to resubmit.php
//try to send a message
if(mail(MY_EMAIL, EMAIL_SUBJECT, setMessageBody($fields_req), "From: $email")) {
echo json_encode(array('message' => 'Your message was successfully submitted.'));
} else {
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.'));
}
When I use:
{
header('location: success.php');
}
else {
header('location: resubmit.php');
}
I'm getting an error message here: Uncaught type error: Cannot read property 'message' of undefined. It shows up under the contactform.addAjaxMessage. Do I need to update that code as well? This is my contact-form.js file:
$(document).ready(function() {
$("#feedbackSubmit").click(function() {
//clear any errors
contactForm.clearErrors();
//do a little client-side validation -- check that each field has a value and e-mail field is in proper format
var hasErrors = false;
$('#feedbackForm input,textarea').not('.optional').each(function() {
if (!$(this).val()) {
hasErrors = true;
contactForm.addError($(this));
}
});
var $email = $('#email');
if (!contactForm.isValidEmail($email.val())) {
hasErrors = true;
contactForm.addError($email);
}
var $phone = $('#phone');
if (!contactForm.isValidPhone($phone.val())) {
hasErrors = true;
contactForm.addError($phone);
}
//if there are any errors return without sending e-mail
if (hasErrors) {
return false;
}
//send the feedback e-mail
$.ajax({
type: "POST",
url: "library/sendmail.php",
data: $("#feedbackForm").serialize(),
success: function(data)
{
contactForm.addAjaxMessage(data.message, false);
//get new Captcha on success
$('#captcha').attr('src', 'library/securimage/securimage_show.php?' + Math.random());
},
error: function(response)
{
contactForm.addAjaxMessage(response.responseJSON.message, true);
}
});
return false;
});
});
//namespace as not to pollute global namespace
var contactForm = {
isValidEmail: function (email) {
var regex = /^([a-zA-Z0-9_.+-])+\#(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
},
/**
* Validates that phone number has 10 digits.
*
* #param {String} phone phone number to validate
* #return {Boolean} if phone number is valid
*/
isValidPhone: function (phone) {
phone = phone.replace(/[^0-9]/g, '');
return (phone.length === 10);
},
clearErrors: function () {
$('#emailAlert').remove();
$('#feedbackForm .help-block').hide();
$('#feedbackForm .form-group').removeClass('has-error');
},
addError: function ($input) {
$input.siblings('.help-block').show();
$input.parent('.form-group').addClass('has-error');
},
addAjaxMessage: function(msg, isError) {
$("#feedbackSubmit").after('<div id="emailAlert" class="alert alert-' + (isError ? 'danger' : 'success') + '" style="margin-top: 5px;">' + $('<div/>').text(msg).html() + '</div>');
}
};
You can simple use header function:
Success case:
Header('location:success.php');
Error case:
Header('location:error.php');
Easy! Just use the PHP header function like so. Just be sure to replace the value of success.php with the actual final location you want users to end up in:
//try to send a message
if(mail(MY_EMAIL, EMAIL_SUBJECT, setMessageBody($fields_req), "From: $email")) {
// echo json_encode(array('message' => 'Your message was successfully submitted.'));
header('Location: success.php');
exit;
} else {
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.'));
}
You can also set the specific HTTP response code by changing this:
header('Location: success.php');
To this:
header('Location: success.php', false, 200);
The 200 response code means “OK” which means the page will be loaded as expected; 100% OK. But you might want to change that to 302 which means the URL has moved temporarilly.
I'm working on a small project with PHP-Websocket.
The Server side is running with this https://github.com/ghedipunk/PHP-Websockets
Server side:
require "PHP-Websockets/websockets.php";
class Server extends WebSocketServer
{
private $_connecting = 'Connecting..';
private $_welcome = 'SOCKET SERVER!';
protected function connected($user)
{
// Send welcome message to user when connected
}
protected function process($user, $message)
{
// data sent from client
$json = json_decode($message);
//prepare data response to client
$response = json_encode(array('type'=>'notify', 'message'=>'Client'.$user->id.' has sent a request.'));
$this->send($user, $response);
}
protected function closed($user)
{
// Alert on server
echo "User $user->id has closed the connection".PHP_EOL;
}
public function __destruct()
{
echo "Server Closed!".PHP_EOL;
}
}
$addr = 'localhost';
$port = '2207';
$server = new Server($addr, $port);
$server->run();
Client Side:
<script>
var uri = "ws://localhost:2207";
function socket_connect(){
socket = new WebSocket(uri);
if(!socket || socket == undefined) return false;
socket.onopen = function(){
console.log('Connected to Server!');
}
socket.onerror = function(){
console.log('Connection Failed!');
}
socket.onclose = function(){
socket_log('Connection Closed! ')
}
socket.onmessage = function(e){
//var response_data = e.data;
var msg = JSON.parse(e.data); //PHP sends Json data to client
console.log(msg.message);
var new_response = '<li>'+msg.message+'</li>;
$('#response').append(new_response);
}
}
function send_data_to_server(data){
if(!socket || socket == undefined) return false;
socket.send(JSON.stringify(data));
}
$(document).ready(function(){
socket_connect();
$('#send_request').click(function(){
send_data_to_server({message: 'Message sent from Client'});
});
});
</script>
<input type="button" id="send_request" value="Send Request to Server" />
<ul id="responses"></ul>
Everything works fine with those code above.
When Client1 sends a request to Server, the Server responses to him instantly. BUT the other clients can not see the response message.
So I want to make it go further : When a client sends a request to the server, the server will response to ALL clients so that all client can see the message.
How can I do that?
Thanks in advance && sorry for my bad English!
When a user connect, you need to add him to an array with every other users.
When one disconnect, remove him from this array.
Then when you want to send a message to every user, iterate on the array and send the message to each connected user.
WebSocketServer class has WebSocketServer::$users variable.
If you iterate over WebSocketServer::$users in split_packet function and then call main process it will work. In latest source code please iterate in line no-405.
//original
if ((preg_match('//u', $message)) || ($headers['opcode']==2)) {
//$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message);
$this->process($user, $message);
} else {
$this->stderr("not UTF-8\n");
}
//have to change
if ((preg_match('//u', $message)) || ($headers['opcode']==2)) {
//$this->stdout("Text msg encoded UTF-8 or Binary msg\n".$message);
foreach($this->users as $usr){
$this->process($usr, $message);
}
} else {
$this->stderr("not UTF-8\n");
}