this is my first project with vue and nodejs so please let me know if I there is a missing information.
I'm trying to develop a group chat with Laravel, Vue js and Pusher.
Database Tables and Relations
I want to create a private Channel for each team which is available.
As soon as you click on the group chat, the existing messages get loaded and shown.
When you send a message, the message gets added to the messages table and is also sent successfully to pusher as you can see here:
Pusher message
The message is also added to the message list on the sender but not on the other team members.
Sender
Other team members
The new message is only shown on the other team members when they reload the page. That means that the echo listener doesn't seem to work. What can I do to fix it? What is wrong?
Here is my code:
ChatApp.vue (root component)
<template>
<div class="chat-container row">
<i class="far fa-comments fa-3x"></i>
<div id="chat-app" class="chat-app">
<div class="row mx-0 h-100 overflow-hidden">
<TeamList :teams="teamList" #selected="startConversationWith"/>
<Conversation :team="selectedTeam" :messages="messages" #new="saveNewMessage"/>
</div>
</div>
</div>
</template>
<script>
import MessageList from './MessageList';
import TeamList from './TeamList';
import Conversation from './Conversation';
import MessageTextBox from './MessageTextBox';
export default {
props: {
user: {
type: Object,
required: true
}
},
data() {
return {
messages: [],
teamList: [],
selectedTeam: null,
}
},
mounted() {
Echo.private('messages.1')
.listen('NewMessage', (e) => {
this.handleIncoming(e.message);
});
axios.get('/teams')
.then((response) => {
this.teamList = response.data;
});
},
methods: {
startConversationWith(team) {
axios.get('/conversation/' + team.id)
.then((response) => {
this.messages = response.data;
this.selectedTeam = team;
});
},
saveNewMessage(text) {
this.messages.push(text);
},
handleIncoming(message) {
this.saveNewMessage(message);
return;
}
},
components: {TeamList, MessageList, MessageTextBox, Conversation}
}
</script>
App/Events/NewMessage.php
<?php
namespace App\Events;
use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* #param Message $message
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('messages.' . $this->message->team_id);
}
public function broadcastWith()
{
$this->message->load('team');
return ["message" => $this->message];
}
}
routes/channels.php
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('messages.{id}', function ($team_id, $message) {
return true;
// return (int) $team->id === (int) $id;
});
Message model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $guarded = [];
public function team()
{
return $this->belongsTo('App\Team');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
ContactsController
namespace App\Http\Controllers;
use App\Events\NewMessage;
use App\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
class ContactsController extends Controller
{
public function getTeams() {
$teams = Auth::user()->teams;
return response()->json($teams);
}
public function getMessagesFor($id)
{
$messages = Message::where('team_id', $id)->get();
return response()->json($messages);
}
public function send(Request $request) {
$message = Message::create([
'team_id' => $request->team_id,
'user_id' => Auth::user()->id,
'message' => $request->text
]);
broadcast(new NewMessage($message));
return response()->json($message);
}
}
bootstrap.js
window._ = require('lodash');
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allows your team to easily build robust real-time web applications.
*/
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: true
});
encrypted is set to true for SSL config. Try setting it to false when configuring laravel echo in bootstrap.js
As you can see in the comments, setting encrypted to false in bootstrap.js solved the problem
Related
I am trying to change the the name of the command buttons in the listView Command set dynamically. I could hard code it and change it in the manifest.json file.
In place of "Command One" and "Command Two" I want to take values from list and change the name of the button.
Create a custom list "CommandList" and add new column "CommandTitle", the default Title field store the command key like "COMMAND_1", CommandTitle field store the command title like "Command One".
Then get the list items and change the default command title in spfx onInit method.
Example code:
import { override } from '#microsoft/decorators';
import { Log } from '#microsoft/sp-core-library';
import {
BaseListViewCommandSet,
Command,
IListViewCommandSetListViewUpdatedParameters,
IListViewCommandSetExecuteEventParameters
} from '#microsoft/sp-listview-extensibility';
import { Dialog } from '#microsoft/sp-dialog';
import * as jQuery from 'jquery';
/**
* If your command set uses the ClientSideComponentProperties JSON input,
* it will be deserialized into the BaseExtension.properties object.
* You can define an interface to describe it.
*/
export interface IListviewbarCommandSetProperties {
// This is an example; replace with your own properties
sampleTextOne: string;
sampleTextTwo: string;
}
const LOG_SOURCE: string = 'ListviewbarCommandSet';
export default class ListviewbarCommandSet extends BaseListViewCommandSet<IListviewbarCommandSetProperties> {
#override
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, 'Initialized ListviewbarCommandSet');
let currentThis=this;
jQuery.ajax({
url: this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('CommandList')/items",
type: "GET",
async:false,
headers: {
"Accept": "application/json;odata=verbose",
},
success: function (data) {
let items:any[] = data.d.results;
items.forEach((item:any)=>{
const compareOneCommand: Command = currentThis.tryGetCommand(item["Title"]);
compareOneCommand.title=item["CommandTitle"];
});
},
error: function (data) {
//alert("Error");
}
});
return Promise.resolve();
}
#override
public onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {
// const compareOneCommand: Command = this.tryGetCommand('COMMAND_1');
// if (compareOneCommand) {
// // This command should be hidden unless exactly one row is selected.
// compareOneCommand.visible = event.selectedRows.length === 1;
// }
}
#override
public onExecute(event: IListViewCommandSetExecuteEventParameters): void {
switch (event.itemId) {
case 'COMMAND_1':
Dialog.alert(`${this.properties.sampleTextOne}`);
break;
case 'COMMAND_2':
Dialog.alert(`${this.properties.sampleTextTwo}`);
break;
default:
throw new Error('Unknown command');
}
}
}
I have followed and set up via #loopback/authentication
But the authentication is added to sequence.ts for all calls.
I am unable to skip authentication for Explorer component
My open source repo: opencommerce/questionnaire-server
Details:
application.ts has
import {BootMixin} from '#loopback/boot';
import {ApplicationConfig} from '#loopback/core';
import {
RestExplorerBindings,
RestExplorerComponent,
} from '#loopback/rest-explorer';
import {RepositoryMixin} from '#loopback/repository';
import {RestApplication} from '#loopback/rest';
import {ServiceMixin} from '#loopback/service-proxy';
import {
AuthenticationComponent,
AuthenticationBindings,
} from '#loopback/authentication';
import {MyAuthStrategyProvider} from './providers';
import * as path from 'path';
import {MySequence} from './sequence';
export class QuestionnaireApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
super(options);
// Set up the custom sequence
this.sequence(MySequence);
// Set up default home page
this.static('/', path.join(__dirname, '../../public'));
// Customize #loopback/rest-explorer configuration here
this.bind(RestExplorerBindings.CONFIG).to({
path: '/explorer',
});
this.component(RestExplorerComponent);
this.projectRoot = __dirname;
this.component(AuthenticationComponent);
this.bind(AuthenticationBindings.STRATEGY).toProvider(
MyAuthStrategyProvider,
);
// Customize #loopback/boot Booter Conventions here
this.bootOptions = {
controllers: {
// Customize ControllerBooter Conventions here
dirs: ['controllers'],
extensions: ['.controller.js'],
nested: true,
},
};
}
}
sequence.ts has
import {inject} from '#loopback/context';
import {
FindRoute,
InvokeMethod,
ParseParams,
Reject,
RequestContext,
RestBindings,
Send,
SequenceHandler,
} from '#loopback/rest';
import {AuthenticationBindings, AuthenticateFn} from '#loopback/authentication';
const SequenceActions = RestBindings.SequenceActions;
export class MySequence implements SequenceHandler {
constructor(
#inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
#inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
#inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
#inject(SequenceActions.SEND) public send: Send,
#inject(SequenceActions.REJECT) public reject: Reject,
#inject(AuthenticationBindings.AUTH_ACTION)
protected authenticateRequest: AuthenticateFn,
) {}
async handle(context: RequestContext) {
try {
const {request, response} = context;
const route = this.findRoute(request);
// This is the important line added to the default sequence implementation
await this.authenticateRequest(request);
// Authentication successful, proceed to invoke controller
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(response, result);
} catch (err) {
this.reject(context, err);
}
}
}
Error while accessing / .
Unhandled error in GET /: 500 Error: The key controller.current.ctor was not bound to any value.
at QuestionnaireApplication.getBinding (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/context.js:225:15)
at RestServer.getBinding (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/context.js:221:33)
at RequestContext.getBinding (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/context.js:221:33)
at RequestContext.getValueOrPromise (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/context.js:260:30)
at resolution_session_1.ResolutionSession.runWithInjection.s (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolver.js:73:24)
at value_promise_1.tryWithFinally (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolution-session.js:89:53)
at Object.tryWithFinally (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/value-promise.js:162:18)
at Function.runWithInjection (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolution-session.js:89:32)
at resolve (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolver.js:66:59)
at value_promise_1.resolveList (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolver.js:144:16)
at Object.resolveList (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/value-promise.js:135:32)
at resolveInjectedArguments (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolver.js:128:28)
at Object.instantiateClass (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolver.js:37:27)
at Binding._getValue (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/binding.js:338:50)
at resolution_session_1.ResolutionSession.runWithBinding.s (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/binding.js:189:90)
at value_promise_1.tryWithFinally (/opt/lampp7.2/htdocs/opencommerce/questionnaire-server/node_modules/#loopback/context/dist/src/resolution-session.js:69:53)
Your custom sequence does not skip the authentication for static route /.
It can be skipped auth as below:
if (!(route instanceof StaticAssetsRoute)) {
// do your login stuff here
}
Now the updated sequence.ts will be:
import {inject} from '#loopback/context';
import {
FindRoute,
InvokeMethod,
ParseParams,
Reject,
RequestContext,
RestBindings,
Send,
SequenceHandler,
StaticAssetsRoute,
} from '#loopback/rest';
import {AuthenticationBindings, AuthenticateFn} from '#loopback/authentication';
const SequenceActions = RestBindings.SequenceActions;
export class MySequence implements SequenceHandler {
constructor(
#inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
#inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
#inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
#inject(SequenceActions.SEND) public send: Send,
#inject(SequenceActions.REJECT) public reject: Reject,
#inject(AuthenticationBindings.AUTH_ACTION)
protected authenticateRequest: AuthenticateFn,
) {}
async handle(context: RequestContext) {
try {
const {request, response} = context;
const route = this.findRoute(request);
// This is the important line added to the default sequence implementation
if (!(route instanceof StaticAssetsRoute)) {
await this.authenticateRequest(request);
}
// Authentication successful, proceed to invoke controller
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(response, result);
} catch (err) {
this.reject(context, err);
}
}
}
One possible issue you can have is that you bind the values after setting your sequence. Then the bindings does not exist when Loopback tries to resolve them. You should put something more like this:
this.bind(RestExplorerBindings.CONFIG).to({
path: '/explorer',
});
this.component(RestExplorerComponent);
this.component(AuthenticationComponent);
this.bind(AuthenticationBindings.STRATEGY).toProvider(
MyAuthStrategyProvider,
);
this.sequence(MySequence);
I'm using the following to determine if the route is static.
private static isStaticRoute(route: ResolvedRoute): boolean {
return route.path.search(/^\/explorer\/*/) === 0;
}
I have the following Symfony 3 controller:
public function register(Request $request)
{
$username=$request->get('username');
$password=$request->get('password');
$email=$request->get('email');
$captchaResponse=$request->get('g-recaptcha-response');
$session =$request->getSession();
$res1 = new Response();
$response_data=array('status'=>0);
if($session->get('captcha')===$captchaResponse)
{
$en = $this->container->get('user_model');
$data=$en->register($username,$password,$email);
$res1->headers->set('Content-Type','text/json');
if($data['status']===false)
{
$response_data['data']="An Internal error Happened";
error_log($data['data']);
}
else if($data['status']===-1)
{
$response_data['data']=$data['data'];
}
else
{
$response_data['status']=1;
$response_data['data']="Please check your mail to confirm your registration.";
/*Send Email*/
$message = \Swift_Message::newInstance()
->setSubject('Please confirm your registration')
->setFrom('symphotest#openmailbox.org')
->setTo($email)
->setBody($this->renderView('emails/confirm.html.twig',array('token'=>$data['data'])))
->addPart(
$this->renderView('emails/registration.txt.twig',array('token'=>$data['data'])),
'text/plain'
);
$this->get('mailer')->send($message);
}
}
else
{
$response_data['data']="You have not given a correct captcha";
}
$res1->setContent(json_encode($response_data));
$session->set('captcha',uniqid());//Generate gibberish in order not to reuse the very same captcha again
return $res1;
}
And I have made the following service:
namespace AppBundle\Models;
use Doctrine\ORM\EntityManager;
use AppBundle\Util\ModelStatus;
use AppBundle\Exceptions\InvalidArgumentException;
use \SwiftMessage;
class UserModel
{
/** #var EntityManager */
private $em;
/** #var \Swift_Mailer */
private $mailer;
/** #var \Twig_Environment */
private $twig;
/**
*
* #param EntityManager $em
* #param \Swift_Mailer $mailer
* #param \Twig_Environment $twig
*/
public function construct(EntityManager $em, \Swift_Mailer $mailer,\Twig_Environment $twig)
{
$this->em=$em;
$this->$mailer=$mailer;
}
/**
* Performs the actions needed for Registration
*
* #param unknown $username
* #param unknown $password
* #param unknown $email
* #param \Swift_Message $registrationMessage
*
* #return ModelStatus
*/
public function register($username,$password,$email)
{
$modelStatus=new ModelStatus();
try
{
/** #var \AppBundle\Entity\UserRepository */
$repository=$this->em->getRepository('AppBundle::Users');
$token=$repository->register($username,$password,$email);
$modelStatus->setData($token);
$modelStatus->setStatus(ModelStatus::STATUS_SUCCESS);
$this->mailer->send($registrationMessage);
$message = Swift_Message::newInstance()
->setSubject('Please confirm your registration')
->setFrom('symphotest#openmailbox.org')
->setTo($email)
->setBody($this->twig->//->renderView('emails/confirm.html.twig',array('token'=>$data['data'])))
->addPart(
$this->renderView('emails/registration.txt.twig',array('token'=>$data['data'])),
'text/plain'
);
$this->get('mailer')->send($message);
}
catch(InvalidArgumentException $arg)
{
$modelStatus->setStatus(ModelStatus::STATUS_FAILURE);
$modelStatus->setMessage($arg->getMessage());
}
catch (\Exception $e)
{
$modelStatus->setStatus(ModelStatus::STATUS_FAILURE);
$modelStatus->setMessage($e->getMessage());
}
return $modelStatus;
}
}
And I am refactoring the following section:
$message = \Swift_Message::newInstance()
->setSubject('Please confirm your registration')
->setFrom('symphotest#openmailbox.org')
->setTo($email)
->setBody($this->renderView('emails/confirm.html.twig',array('token'=>$data['data'])))
->addPart(
$this->renderView('emails/registration.txt.twig',array('token'=>$data['data'])),
'text/plain'
);
$this->get('mailer')->send($message);
Into the Model method register.
But As you can see I render some twig templates and I do know the best Option on how to do it. So far I thought the following options:
To render the templates as string and pass them to the the register method, create and send the email there.
Load the twig rendering service into the model and the render into the model. If not exists create one.
In the second bullet I may need to load the Twig rendering engine into a service. How can I do that?
In the end having the constructor like this:
public function __construct(EntityManager $em, \Swift_Mailer $mailer,\Twig_Environment $twig)
{
$this->em=$em;
$this->mailer=$mailer;
$this->twig=$twig;
}
And loading the service like this:
user_model:
class: AppBundle\Models\UserModel
arguments: ['#doctrine.orm.entity_manager','#mailer','#twig']
Seems that solved the problem.
I want to know in Twig from what URI the user came. For example:
The user is on the page /en/terms-of-use ('app_default_terms')
User clicks on login
On the register page is a hidden input to send the user back from where he came <input type="hidden" name="_target_path" value="{{ get_uri_from_where_he_came() }}" />
He submits the form and is sent back.
My question is, what is the twig function for this?
Using symfony framework, you can access a special global variable app which hold the request among other attributes.
see http://symfony.com/doc/current/book/templating.html#global-template-variables
So you could use something like :
{% set referer = app.request.server.get('http-referer')|default('/') %}
<input type="hidden" name="_target_path" value="{{ referer }}" />
My suggestion is a bit over-killing but ensures that you don't rely on arbitrary data coming from users.
The idea
You always save the previous route on your application using an event listener; and you implement a /reload path that will send the user back to that route. On login success, you just have to redirect your user to that /reload path and you're good to go.
The implementation
Change namespaces to fit with your application.
LastRouteListener.php
<?php
namespace Fuz\QuickStartBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Fuz\QuickStartBundle\Services\Routing;
class LastRouteListener implements EventSubscriberInterface
{
protected $routing;
public function __construct(Routing $routing)
{
$this->routing = $routing;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
try {
$currentRoute = $this->routing->getCurrentRoute($request);
} catch (ResourceNotFoundException $ex) {
return;
}
if (is_null($currentRoute)) {
return;
}
$session = $request->getSession();
$previousRoute = $session->get('current_route', array());
if ($currentRoute == $previousRoute) {
return;
}
$session->set('previous_route', $previousRoute);
$session->set('current_route', $currentRoute);
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
);
}
}
services.yml
parameters:
# ...
quickstart.last_route.listener.class: Fuz\QuickStartBundle\EventListener\LastRouteListener
services:
# ...
quickstart.last_route.listener:
class: %quickstart.last_route.listener.class%
arguments: [#quickstart.routing]
tags:
- { name: kernel.event_subscriber }
ReloadController.php
<?php
namespace Fuz\QuickStartBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Fuz\QuickStartBundle\Base\BaseController;
class ReloadController extends BaseController
{
/**
* Get back to the previous route
*
* #Route("/reload", name="reload")
* #Method({"GET"})
*/
public function reloadAction(Request $request)
{
if ($request->getSession()->has('previous_route')) {
$route = $request->getSession()->get('previous_route');
$route['params']['_locale'] = $request->getLocale();
return $this->redirect($this->generateUrl($route['name'], $route['params']));
}
return $this->redirect($this->generateUrl('home'));
}
}
Live
You can clone "symfony-quickstart" from my GitHub if you want to see this implementation live.
https://github.com/ninsuo/symfony-quickstart
Symfony 5.4
This worked for me.
app.request.headers.get('referer')
In FMS i want to use Shared objects to send messages in a chat application because its in real time.
My question is...How do you use Shared Objects to send messages back and forth to users in a live chat application? Would this require Server-side scripting, client or both?
You'll only need to write some code on the server-side for specific functionalities, such as security features (if not all the users can send messages for example).
On the client side, you need to:
connect to the server;
get the shared object from the server. If it does not exist when you ask for it, it will be created;
add a listener on it for SyncEvent.
From there, every time a client will add, update or delete a property of the sharedObject using the setProperty() method, all the connected clients will receive a SyncEvent.
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;
import flash.net.NetConnection;
import flash.net.SharedObject;
public class Chat extends EventDispatcher
{
public var messages:Array;
private var connection:NetConnection;
private var chat_so:SharedObject;
public function Chat()
{
super();
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
messages = [];
}
public function connect(uri:String):void
{
connection.connect(uri);
}
public function addMessage(value:String):void
{
chat_so.setProperty(messages.length.toString(), value);
}
private function setUpSharedObject():void
{
chat_so = SharedObject.getRemote("chat", connection.uri);
chat_so.addEventListener(SyncEvent.SYNC, onSync);
chat_so.client = this;
}
private function onNetStatus(event:NetStatusEvent):void
{
if (event.info.code == "NetConnection.Connect.Success")
{
setUpSharedObject();
}
else
{
trace(event.info.code + ": " + event.info.description);
}
}
private function onSync(event:SyncEvent):void
{
for (var i:int = 0; i < event.changeList.length; i++)
{
var code:String = event.changeList[i].code;
switch(code)
{
case "change":
case "success":
{
messages.push();
dispatchEvent(new Event(Event.CHANGE));
break;
}
case "clear":
{
messages = [];
break;
}
case "delete":
default:
{
break;
}
}
}
}
}
}