Skip to content

Commit

Permalink
Update chat example.
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Jul 13, 2015
1 parent f7657ab commit 70e558c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
28 changes: 11 additions & 17 deletions examples/chat/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@
}
return name;
}
var client = hprose.Client.create('ws://127.0.0.1:8080/', ['getAllUsers', 'getUpdateUsers', 'getMessage', 'sendMessage', 'broadcast']);
var client = hprose.Client.create('ws://127.0.0.1:8080/', ['getAllUsers', 'sendMessage', 'broadcast']);
var me = inputName();

client.addRemoteEvent('message', me, function(message) {
if (message !== null) {
message = message.replace(/\n/g, '\n<br />');
var dialog = document.getElementById('dialog');
dialog.innerHTML = dialog.innerHTML + '<div class="speech_item"><p class="triangle-isosceles top">' + message + '</p></div>';
}
});

function updateUserList(users) {
var userlist = document.getElementById('userlist');
var to = userlist.options[userlist.selectedIndex].value;
Expand All @@ -47,23 +55,9 @@
}
}
userlist.innerHTML = options;
client.getUpdateUsers(me, updateUserList, function() {
client.getUpdateUsers(me, updateUserList);
});
}

function getMessage(message) {
if (message !== null) {
message = message.replace(/\n/g, '\n<br />');
var dialog = document.getElementById('dialog');
dialog.innerHTML = dialog.innerHTML + '<div class="speech_item"><p class="triangle-isosceles top">' + message + '</p></div>';
}
client.getMessage(me, getMessage, function() {
client.getMessage(me, getMessage);
});
}

getMessage(null);
client.addRemoteEvent('updateUsers', me, updateUserList);

client.getAllUsers(updateUserList);

Expand All @@ -82,4 +76,4 @@
}
</script>
</body>
</html>
</html>
6 changes: 3 additions & 3 deletions examples/chat/chat_server.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function online($who) {
$this->broadcast($who, $who . " is online.");
}
if ($this->timer == null) {
$this->timer = swoole_timer_add(1000, function() {
$this->timer = swoole_timer_tick(1000, function() {
$users = $this->getAllUsers();
foreach ($users as $user) {
if (!isset($this->getMessage[$user]) &&
Expand Down Expand Up @@ -95,7 +95,7 @@ public function getAllUsers() {
return array_keys($this->gens);
}

public function getUpdateUsers($who) {
public function updateUsers($who) {
$this->online($who);
$getUpdateUsers = new StdClass();
$getUpdateUsers->completer = new HproseCompleter();
Expand All @@ -110,7 +110,7 @@ public function getUpdateUsers($who) {
return $getUpdateUsers->completer->future();
}

public function getMessage($who) {
public function message($who) {
$this->online($who);
if (isset($this->messages[$who])) {
$message = $this->messages[$who];
Expand Down
Loading

0 comments on commit 70e558c

Please sign in to comment.