forked from zeroc-ice/ice-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionI.php
237 lines (213 loc) · 5.88 KB
/
SessionI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
# **********************************************************************
#
# Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
#
# **********************************************************************
class JavaScriptSession
{
public $jsontype;
public $id;
function __construct($id)
{
$this->id = $id;
$this->jsontype = "Session";
}
}
/**
*
* The Session class holds the proxy to the PollingChatSession and permits
* interaction with the chat room.
*
**/
class Session
{
private $_communicator = 0;
private $_chatsession = 0;
//
// Constructor
//
function __construct($communicator)
{
$this->_communicator = $communicator;
if($this->isConnected())
{
$this->_chatsession = $this->_communicator->stringToProxy($_SESSION["chatsession"]);
$this->_chatsession = $this->_chatsession->ice_uncheckedCast("::PollingChat::PollingChatSession");
}
}
//
// Join the chat.
//
function login($username, $password)
{
try
{
//
// Initialize a proxy to the PollingChatSessionFactory.
//
$chatsessionfactory = $this->_communicator->propertyToProxy("PollingChatSessionFactory");
//
// Cast the proxy to the appropriate type.
//
$chatsessionfactory = $chatsessionfactory->ice_uncheckedCast("::PollingChat::PollingChatSessionFactory");
//
// Create a new session.
//
$this->_chatsession = $chatsessionfactory->create($username, $password);
//
// Override session proxy's endpoints if necessary
//
if($this->_communicator->getProperties()->getProperty("PollingChatSessionFactory.OverrideEndpoints") != "")
{
$ident = Ice_identityToString($this->_chatsession->ice_getIdentity());
$endpoints = $this->_communicator->getProperties()->getProperty(
"PollingChatSessionFactory.OverrideEndpoints");
$this->_chatsession = $this->_communicator->stringToProxy(
$ident . ":wss -h zeroc.com -p 443 -r /demo-proxy/chat/poll");
}
//
// Mark the PHP session as connected.
//
$this->setConnected(true);
return new JavaScriptSession(session_id()); // Return the sessionId to the client.
}
catch(Ice_Exception $ex)
{
$ex->jsontype = get_class($ex);
return $ex;
}
}
//
// Leave the chat.
//
function logout()
{
if($this->isConnected())
{
try
{
$this->_chatsession->destroy();
}
catch(Ice_Exception $ex)
{
}
$this->setConnected(false);
}
}
//
// Send a message to the chat.
//
function send($message)
{
$this->checkIsConnected();
try
{
return $this->_chatsession->send($message);
}
catch(Ice_Exception $ex)
{
$ex->jsontype = get_class($ex);
return $ex;
}
}
//
// Get the initial set of users in the chat room.
//
function getInitialUsers()
{
$this->checkIsConnected();
try
{
return $this->_chatsession->getInitialUsers();
}
catch(Ice_Exception $ex)
{
$ex->jsontype = get_class($ex);
return $ex;
}
}
//
// Get state changes in the room since the previous call.
//
function getUpdates()
{
$this->checkIsConnected();
try
{
$updates = $this->_chatsession->getUpdates();
//
// Reindex the array to ensure that JSON treat is as an array
// and not like an associative map (Object)
//
$data = array();
$length = count($updates);
for($i = 0; $i < $length; $i++)
{
$update = $updates[$i];
$update->jsontype = get_class($update);
$data[] = $update;
}
return $data;
}
catch(Ice_Exception $ex)
{
$ex->jsontype = get_class($ex);
return $ex;
}
}
//
// Set the status of the session.
//
function setConnected($connected)
{
if($connected)
{
//
// Save the session proxy to the PHP $_SESSION object as a string proxy.
//
$_SESSION["chatsession"] = $this->_communicator->proxyToString($this->_chatsession);
//
// Set the connected flag to true (Connected).
//
$_SESSION["connected"] = "true";
}
else
{
//
// Set the connected flag to false (Disconnected)
//
$_SESSION["connected"] = "false";
//
// Erase the chatsession string proxy.
//
$_SESSION["chatsession"] = "";
//
// Erase the reference to the chatsession proxy.
//
$this->_chatsession = 0;
}
}
//
// Helper method to check if a user is connected. Return true if connected,
// false otherwise.
//
function isConnected()
{
return isset($_SESSION["connected"]) && $_SESSION["connected"] == "true";
}
//
// This method checks if the user is connected. If the user is not connected,
// it raises Ice_ObjectNotExistException; otherwise, it does nothing.
//
function checkIsConnected()
{
if(!$this->isConnected())
{
$ex = new Ice_ObjectNotExistException();
$ex->jsontype = get_class($ex);
throw $ex;
}
}
}
?>