├── Examples ├── chatExample.go ├── chatExample.html ├── chatExample.js ├── friendExample.html └── friendExample.js ├── JS_Gopher.png ├── LICENSE ├── README.md ├── authTest.html ├── authTest.js ├── src ├── gopher-client.js ├── gopher-voice.js └── min │ ├── gopher-client.min.js │ └── gopher-voice.min.js ├── varsTest.html └── varsTest.js /Examples/chatExample.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/hewiefreeman/GopherGameServer" 5 | "github.com/hewiefreeman/GopherGameServer/core" 6 | "fmt" 7 | ) 8 | 9 | func main() { 10 | // Set the core server settings 11 | settings := gopher.ServerSettings{ 12 | ServerName: "!s!", 13 | MaxConnections: 0, 14 | 15 | HostName: "localhost", 16 | HostAlias: "localhost", 17 | IP: "localhost", 18 | Port: 8080, 19 | 20 | AdminLogin: "admin", 21 | AdminPassword: "admin", 22 | } 23 | 24 | // Make a Room type and set broadcasts and callbacks 25 | chatRoomType := core.NewRoomType("chat", true) 26 | chatRoomType.EnableBroadcastUserEnter().EnableBroadcastUserLeave(). 27 | SetUserEnterCallback(onEnterChat).SetUserLeaveCallback(onLeaveChat) 28 | 29 | // Open a Room 30 | _, roomErr := core.NewRoom("chatExample", "chat", false, 0, "") 31 | if roomErr != nil { 32 | fmt.Println("Error while opening Room:", roomErr) 33 | return 34 | } 35 | 36 | gopher.Start(&settings) 37 | } 38 | 39 | func onEnterChat(room *core.Room, user *core.RoomUser) { 40 | // Example of using parameters to send a welcome message to the entering User 41 | message := "Welcome! Please read the chat room rules, and have fun!" 42 | messageErr := room.ServerMessage(message, core.ServerMessageNotice, []string{user.User().Name()}) 43 | if messageErr != nil { 44 | fmt.Println("Error while messaging User:", messageErr) 45 | } 46 | } 47 | 48 | func onLeaveChat(room *core.Room, user *core.RoomUser) { 49 | // ... 50 | 51 | // To convert RoomUser to User: 52 | // u := user.User() 53 | } 54 | -------------------------------------------------------------------------------- /Examples/chatExample.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Gopher Server Chat Example 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Login:

14 |

User Name: Password:

15 |

Guest

16 |

Remember me

17 |
18 |

Chat Message

19 |

message:

20 |
21 |

Private Message

22 |

User: message:

23 | 24 | 25 | -------------------------------------------------------------------------------- /Examples/chatExample.js: -------------------------------------------------------------------------------- 1 | var gc = null; 2 | window.onload = loaded; 3 | 4 | function loaded(e){ 5 | gc = gopherClient; 6 | 7 | //SERVER CONNECTION LISTENERS 8 | gc.addEventListener(gc.events.connected, connected); 9 | gc.addEventListener(gc.events.disconnected, disconnected); 10 | //SIGNUP/LOGIN LISTENERS 11 | gc.addEventListener(gc.events.login, onLogin); 12 | gc.addEventListener(gc.events.logout, onLogout); 13 | //ROOM LISTENERS 14 | gc.addEventListener(gc.events.joined, joinedRoom) 15 | //CHAT LISTENERS 16 | gc.addEventListener(gc.events.chatMessage, onChat) 17 | gc.addEventListener(gc.events.privateMessage, onPM) 18 | gc.addEventListener(gc.events.serverMessage, onServerMessage) 19 | //CONNECT 20 | gc.connect("localhost", 8080, false); 21 | } 22 | 23 | function connected(){ 24 | console.log("connect success!"); 25 | 26 | //ENABLE LOGIN 27 | document.getElementById("loginBtn").onclick = function(){ 28 | var login = document.getElementById("loginText").value; 29 | var pass = document.getElementById("passText").value; 30 | var rememberMe = document.getElementById("rememberCheck").checked; 31 | var guest = document.getElementById("guestCheck").checked; 32 | gc.login(login, pass, rememberMe, guest); 33 | }; 34 | } 35 | 36 | function disconnected(){ 37 | console.log("DISCONNECTED!"); 38 | } 39 | 40 | ////////////////// LOGIN ////////////////////////////// 41 | function onLogin(userName, error){ 42 | if(error != null){ 43 | console.log("Error: [ID - "+error.id+"], [Message - '"+error.m+"']"); 44 | }else{ 45 | console.log("Logged in as: "+userName); 46 | //ENTER TEST ROOM 47 | gc.joinRoom("chatExample") 48 | } 49 | } 50 | 51 | ////////////////// LOGOUT ////////////////////////////// 52 | function onLogout(success, error){ 53 | if(error != null){ 54 | console.log("Error: [ID - "+error.id+"], [Message - '"+error.m+"']"); 55 | }else{ 56 | console.log("You have been logged out"); 57 | } 58 | } 59 | 60 | ////////////////// JOINED ROOM ////////////////////////////// 61 | function joinedRoom(success, error){ 62 | if(error != null){ 63 | console.log("Error: [ID - "+error.id+"], [Message - '"+error.m+"']"); 64 | }else{ 65 | console.log("Joined room!"); 66 | 67 | //SEND CHAT MESSAGE 68 | document.getElementById("chatBtn").onclick = function(){ 69 | var message = document.getElementById("chatText").value; 70 | gc.chatMessage(message); 71 | }; 72 | 73 | //SEND PRIVATE MESSAGE 74 | document.getElementById("pmBtn").onclick = function(){ 75 | var user = document.getElementById("pmUserText").value; 76 | var message = document.getElementById("pmMessageText").value; 77 | gc.privateMessage(user, message); 78 | }; 79 | } 80 | } 81 | 82 | ////////////////// CHAT MESSAGES ////////////////////////////// 83 | function onChat(user, message){ 84 | console.log("["+gc.getRoom()+"]"+user+": "+message); 85 | } 86 | 87 | ////////////////// PRIVATE MESSAGES ////////////////////////////// 88 | function onPM(from, to, message){ 89 | console.log(from+" [to] "+to+": "+message); 90 | } 91 | 92 | ////////////////// SERVER MESSAGES ////////////////////////////// 93 | function onServerMessage(type, message){ 94 | console.log("Server Message ("+gc.serverMessageNames[type]+"): "+message); 95 | } 96 | -------------------------------------------------------------------------------- /Examples/friendExample.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Gopher Server Friending Example 7 | 8 | 9 | 10 | 11 | 12 | 13 |

Login:

14 |

User Name: Password:

15 |


16 |

Send Request:

17 |

User Name:

18 |
19 |

Accept Request:

20 |

User Name:

21 |
22 |

Decline Request:

23 |

User Name:

24 |
25 |

Remove Friend:

26 |

User Name:

27 |
28 |

Change Your Status:

29 |

34 | 35 | 36 | -------------------------------------------------------------------------------- /Examples/friendExample.js: -------------------------------------------------------------------------------- 1 | var gc = null; 2 | window.onload = loaded; 3 | 4 | function loaded(e){ 5 | gc = gopherClient; 6 | 7 | //SERVER CONNECTION LISTENERS 8 | gc.addEventListener(gc.events.connected, connected); 9 | gc.addEventListener(gc.events.disconnected, disconnected); 10 | //SIGNUP/LOGIN LISTENERS 11 | gc.addEventListener(gc.events.login, onLogin); 12 | gc.addEventListener(gc.events.logout, onLogout); 13 | //FRIEND LISTENERS 14 | gc.addEventListener(gc.events.friendRequested, onRequestFriend);// WHEN YOU REQUEST A FRIEND 15 | gc.addEventListener(gc.events.friendAccepted, onAcceptFriend);// WHEN YOU ACCEPT A REQUEST 16 | gc.addEventListener(gc.events.friendDeclined, onDeclineFriend);// WHEN YOU DECLINE A REQUEST 17 | gc.addEventListener(gc.events.friendRemoved, onRemoveFriend);// WHEN A FRIEND GETS REMOVED OR WHEN A USER DECLINES YOUR REQUEST 18 | gc.addEventListener(gc.events.friendRequestReceived, onReceiveFriendRequest);// WHEN YOU RECIEVE A FRIEND REQUEST FROM ANOTHER USER 19 | gc.addEventListener(gc.events.friendRequestAccepted, onReceiveFriendAccept);// WHEN YOUR REQUEST TO ANOTHER USER IS ACCEPTED 20 | gc.addEventListener(gc.events.friendStatusChanged, onFriendStatusChange);// WHEN A FRIEND'S STATUS CHANGES 21 | gc.addEventListener(gc.events.statusChanged, onStatusChange); 22 | //CONNECT 23 | gc.connect("localhost", 8080, false); 24 | } 25 | 26 | function connected(){ 27 | console.log("connect success!"); 28 | //ENABLE UI 29 | 30 | //LOGIN 31 | document.getElementById("loginBtn").onclick = function(){ 32 | var login = document.getElementById("loginText").value; 33 | var pass = document.getElementById("passText").value; 34 | gc.login(login, pass); 35 | }; 36 | 37 | //SEND REQUEST 38 | document.getElementById("requestBtn").onclick = function(){ 39 | if(gc.isLoggedIn){ 40 | var friend = document.getElementById("requestText").value; 41 | gc.requestFriend(friend); 42 | }else{ 43 | console.log("Must be logged in to request a friend") 44 | } 45 | }; 46 | 47 | //ACCEPT REQUEST 48 | document.getElementById("acceptBtn").onclick = function(){ 49 | if(gc.isLoggedIn){ 50 | var friend = document.getElementById("acceptText").value; 51 | gc.acceptFriend(friend); 52 | }else{ 53 | console.log("Must be logged in to accept a friend") 54 | } 55 | }; 56 | 57 | //DECLINE REQUEST 58 | document.getElementById("declineBtn").onclick = function(){ 59 | if(gc.isLoggedIn){ 60 | var friend = document.getElementById("declineText").value; 61 | gc.declineFriend(friend); 62 | }else{ 63 | console.log("Must be logged in to decline a friend") 64 | } 65 | }; 66 | 67 | //REMOVE FRIEND 68 | document.getElementById("removeBtn").onclick = function(){ 69 | if(gc.isLoggedIn){ 70 | var friend = document.getElementById("removeText").value; 71 | gc.removeFriend(friend); 72 | }else{ 73 | console.log("Must be logged in to remove a friend") 74 | } 75 | }; 76 | 77 | //CHANGE USER STATUS 78 | document.getElementById("statusSelect").onchange = function() { 79 | //change the tag innerHTML checking the selected value of the select 80 | var status = parseInt(document.getElementById("statusSelect").value); 81 | gc.changeStatus(status); 82 | } 83 | } 84 | 85 | function disconnected(){ 86 | console.log("DISCONNECTED!"); 87 | 88 | } 89 | 90 | ////////////////// LOGIN ////////////////////////////// 91 | function onLogin(userName, error){ 92 | if(error != null){ 93 | console.log("Error:"); 94 | console.log(error); 95 | }else{ 96 | console.log("Logged in as: "+userName); 97 | } 98 | } 99 | 100 | ////////////////// LOGOUT ////////////////////////////// 101 | function onLogout(success, error){ 102 | if(error != null){ 103 | console.log("Error:"); 104 | console.log(error); 105 | }else{ 106 | console.log("You have been logged out"); 107 | } 108 | } 109 | 110 | ////////////////// REQUESTED FRIEND ////////////////////////////// 111 | function onRequestFriend(friendName, error){ 112 | if(error != null){ 113 | console.log("Error:"); 114 | console.log(error) 115 | }else{ 116 | console.log("Sent friend request to: "+friendName); 117 | } 118 | } 119 | 120 | ////////////////// ACCEPTED FRIEND ////////////////////////////// 121 | function onAcceptFriend(friendName, error){ 122 | if(error != null){ 123 | console.log("Error:"); 124 | console.log(error) 125 | }else{ 126 | console.log("Accepted "+friendName+" as a friend"); 127 | } 128 | } 129 | 130 | ////////////////// DECLINED FRIEND ////////////////////////////// 131 | function onDeclineFriend(friendName, error){ 132 | if(error != null){ 133 | console.log("Error:"); 134 | console.log(error) 135 | }else{ 136 | console.log("Declined "+friendName+" as a friend"); 137 | } 138 | } 139 | 140 | ////////////////// DECLINED FRIEND ////////////////////////////// 141 | function onRemoveFriend(friendName, error){ 142 | if(error != null){ 143 | console.log("Error:"); 144 | console.log(error) 145 | }else{ 146 | console.log(friendName+" was removed from your friends list"); 147 | } 148 | } 149 | 150 | ////////////////// STATUS CHANGE ////////////////////////////// 151 | function onStatusChange(status, error){ 152 | if(error != null){ 153 | console.log("Error:"); 154 | console.log(error) 155 | }else{ 156 | console.log("Changed your status to '"+gc.statusName(status)+"'"); 157 | } 158 | } 159 | 160 | ////////////////// RECEIVED FRIEND REQUEST ////////////////////////////// 161 | function onReceiveFriendRequest(friendName){ 162 | console.log("Recieved friend request from: "+friendName); 163 | } 164 | 165 | ////////////////// RECEIVED FRIEND REQUEST ACCEPT ////////////////////////////// 166 | function onReceiveFriendAccept(friendName){ 167 | console.log(friendName+" accepted your friend request"); 168 | } 169 | 170 | ////////////////// FRIEND STATUS CHANGE ////////////////////////////// 171 | function onFriendStatusChange(friendName, status){ 172 | console.log(friendName+" changed their status to '"+gc.statusName(status)+"'"); 173 | } 174 | -------------------------------------------------------------------------------- /JS_Gopher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hewiefreeman/GopherClientJS/88be1c1c3e21ec161f776927ac80bd60e34e70aa/JS_Gopher.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2019] [Dominique Claude Debergue] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gopher Game Server - Javascript Client 2 | 3 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/hewiefreeman/GopherGameServer?status.svg)](https://godoc.org/github.com/hewiefreeman/GopherGameServer) 4 | 5 | This is the JavaScript client API for [Gopher Game Server](https://github.com/hewiefreeman/GopherGameServer). It provides all the functionalities to use Gopher Game Server with vanilla, or any JavaScript libraries. Source files can be found in the `src` folder, examples in the `example` folder, and minified source files in the `src/min` folder. 6 | 7 | # :books: Usage 8 | 9 | ## Table of Contents 10 | 11 | 1) [**Getting Started**](https://github.com/hewiefreeman/GopherClientJS/wiki/Getting-Started) 12 | - [Set-Up](https://github.com/hewiefreeman/GopherClientJS/wiki/Getting-Started#set-up) 13 | - [Connecting to the Server](https://github.com/hewiefreeman/GopherClientJS/wiki/Getting-Started#connecting-to-the-server) 14 | 2) [**Client Actions**](https://github.com/hewiefreeman/GopherClientJS/wiki/Client-Actions) 15 | - [Login & Logout](https://github.com/hewiefreeman/GopherClientJS/wiki/Client-Actions#login--logout) 16 | - [Rooms](https://github.com/hewiefreeman/GopherClientJS/wiki/Client-Actions#rooms) 17 | - [User Variables](https://github.com/hewiefreeman/GopherClientJS/wiki/Client-Actions#user-variables) 18 | - [Messaging](https://github.com/hewiefreeman/GopherClientJS/wiki/Client-Actions#messaging) 19 | - [Executing Custom Client Actions](https://github.com/hewiefreeman/GopherClientJS/wiki/Client-Actions#executing-custom-client-actions) 20 | 3) [**SQL Authentication**](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication) 21 | - [Creating accounts](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#creating-accounts) 22 | - [Deleting Accounts](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#deleting-accounts) 23 | - [Login](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#login) 24 | - [Changing Passwords](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#changing-passwords) 25 | - [Changing Custom Account Info](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#changing-custom-account-info) 26 | - [Auto-Login (Remember Me)](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#auto-login-remember-me) 27 | - [Friending](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#friending) 28 | - [User Status](https://github.com/hewiefreeman/GopherClientJS/wiki/SQL-Authentication#user-status) 29 | 30 | # :milky_way: Contributions 31 | Contributions are open and welcomed! Help is needed for everything from documentation, cleaning up code, performance enhancements, client APIs (in any laguage) and more. Don't forget to show your support by starring or following the project! 32 | 33 | If you want to make a client API in an unsupported language and want to know where to start and/or have any questions, feel free to [email me](mailto:dominiquedebergue@gmail.com?subject=[GitHub]%20Gopher%20Game%20Server)! 34 | 35 | Please read the following articles before submitting any contributions or filing an Issue: 36 | 37 | - [Contribution Guidlines](https://github.com/hewiefreeman/GopherGameServer/blob/master/CONTRIBUTING.md) 38 | - [Code of Conduct](https://github.com/hewiefreeman/GopherGameServer/blob/master/CODE_OF_CONDUCT.md) 39 | -------------------------------------------------------------------------------- /authTest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Gopher Server Authorization Test 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /authTest.js: -------------------------------------------------------------------------------- 1 | var gc = null; 2 | window.onload = loaded; 3 | 4 | function loaded(e){ 5 | gc = gopherClient; 6 | 7 | //SERVER CONNECTION LISTENERS 8 | gc.addEventListener(gc.events.connected, connected); 9 | gc.addEventListener(gc.events.disconnected, disconnected); 10 | //SIGNUP/LOGIN LISTENERS 11 | gc.addEventListener(gc.events.login, onLogin); 12 | gc.addEventListener(gc.events.logout, onLogout); 13 | gc.addEventListener(gc.events.signup, onSignup); 14 | //AUTOLOGGING LISTENERS 15 | gc.addEventListener(gc.events.autologInit, onAutoLogInit); 16 | gc.addEventListener(gc.events.autologFailed, onAutoLogFail); 17 | gc.addEventListener(gc.events.autologNoFile, onAutoLogNoFile); 18 | //ACCOUNT ACTION LISTENERS 19 | gc.addEventListener(gc.events.accountInfoChange, onInfoChange); 20 | gc.addEventListener(gc.events.accountDelete, onDeleteAccount); 21 | gc.addEventListener(gc.events.passwordChange, onPasswordChange); 22 | //ROOM LISTENERS 23 | //gc.addEventListener(gc.events.joined, joined); 24 | //CONNECT 25 | gc.connect("localhost", 8080, false); 26 | } 27 | 28 | function closeMic(){ 29 | gc.voiceChat.closeMic(); 30 | } 31 | 32 | function connected(){ 33 | console.log("connected!"); 34 | 35 | //userName, password, customCols 36 | gc.signup("Cheekspreadr", "l2pdmger", {email: "hewiefreeman@gmail.com"}); 37 | 38 | //userName, isGuest, password, rememberMe, customCols 39 | //gc.login("dominiquedebergue@gmail.com", "l2pdmger"); 40 | 41 | //userName, password, customCols 42 | //gc.deleteAccount("Bobby McGee", "ftwomg"); 43 | } 44 | 45 | function disconnected(){ 46 | console.log("DISCONNECTED!"); 47 | } 48 | 49 | function onSignup(success, error){ 50 | if(success){ 51 | console.log("Sign up success!"); 52 | }else{ 53 | console.log(error); 54 | } 55 | } 56 | 57 | function onLogin(loginName, error){ 58 | if(loginName){ 59 | console.log("Login success! As:"+loginName); 60 | 61 | //password, newPassword, customCols 62 | //gc.changePassword("ftwomg", "ftwdmg"); 63 | 64 | //password, customCols 65 | //gc.changeAccountInfo("l2pdmger", {email:"dominiquedebergue@gmail.com"}); 66 | 67 | // 68 | //gc.logout(); 69 | 70 | }else{ 71 | console.log(error) 72 | } 73 | } 74 | 75 | function onLogout(success, error){ 76 | if(success){ 77 | console.log("You have been logged out."); 78 | 79 | //password, newPassword, customCols 80 | //gc.changePassword("ftwomg", "ftwdmg"); 81 | 82 | }else{ 83 | console.log(error) 84 | } 85 | } 86 | 87 | function onDeleteAccount(success, error){ 88 | if(success){ 89 | console.log("Delete account success!"); 90 | }else{ 91 | console.log(error); 92 | } 93 | } 94 | 95 | function onPasswordChange(success, error){ 96 | if(success){ 97 | console.log("Password change success!"); 98 | }else{ 99 | console.log(error); 100 | } 101 | } 102 | 103 | function onInfoChange(success, error){ 104 | if(success){ 105 | console.log("Password change success!"); 106 | }else{ 107 | console.log(error); 108 | } 109 | } 110 | 111 | function onAutoLogInit(){ 112 | console.log("Initializing Auto-Log"); 113 | } 114 | 115 | function onAutoLogFail(){ 116 | console.log("Auto-Log failed"); 117 | } 118 | 119 | function onAutoLogNoFile(){ 120 | console.log("Auto-Log Has No File"); 121 | //userName, isGuest, password, rememberMe, customCols 122 | //gc.login("dominiquedebergue@gmail.com", "l2pdmger", true); 123 | } 124 | 125 | 126 | /*function joined(success, error){ 127 | if(success){ 128 | console.log("Join room success!"); 129 | gc.voiceChat.setBufferSize(gc.voiceChat.BUFFER_SIZE_LARGE); 130 | //MAKE BUTTON CLICKABLE TO SEND AUDIO STREAM 131 | document.getElementById("box").onclick = function(){ 132 | gc.voiceChat.startVoiceChannels(); 133 | } 134 | var micOn = false; 135 | window.onkeydown = function(e){ 136 | if(!micOn){ 137 | console.log(gc.voiceChat.openMic()); 138 | micOn = true; 139 | } 140 | } 141 | window.onkeyup = function(e){ 142 | if(micOn){ 143 | console.log(gc.voiceChat.closeMic()); 144 | micOn = false; 145 | } 146 | } 147 | ////////////////////////////////////////////// 148 | 149 | }else{ 150 | console.log(error) 151 | } 152 | } 153 | */ 154 | -------------------------------------------------------------------------------- /src/gopher-client.js: -------------------------------------------------------------------------------- 1 | var gopherClient = new GopherServerClient(); 2 | 3 | function GopherServerClient() { 4 | var self = this; 5 | 6 | // INITIAL CHECKS 7 | this.browserVoiceSupport = false; 8 | this.voiceChat = null; 9 | 10 | // CHECK WEBSOCKET SUPPORT 11 | if(typeof window.WebSocket === "undefined"){ return null; } // WebSocket is required! 12 | 13 | // CHECK MICROPHONE/SOUND SUPPORT 14 | if(typeof navigator.mediaDevices.getUserMedia !== "undefined" && typeof GopherVoiceChat !== "undefined"){ 15 | this.browserVoiceSupport = true; 16 | this.voiceChat = new GopherVoiceChat(); 17 | } 18 | 19 | // INITIALIZE OBJECTS 20 | this.ip = ""; 21 | this.port = 0; 22 | this.socketURL = ""; 23 | this.ssl = false; 24 | this.socket = null; 25 | 26 | // 27 | this.connected = false; 28 | this.loggedIn = false; 29 | this.rememberMe = false; 30 | this.guest = false; 31 | this.userName = ""; 32 | this.roomName = ""; 33 | this.status = 0; 34 | this.friends = {}; 35 | this.userVars = {}; 36 | 37 | // GENERAL DEFINITIONS 38 | this.clientActionDefs = { 39 | signup: "s", 40 | deleteAccount: "d", 41 | changePassword: "pc", 42 | changeAccountInfo: "ic", 43 | login: "li", 44 | logout: "lo", 45 | joinRoom: "j", 46 | leaveRoom: "lr", 47 | createRoom: "r", 48 | deleteRoom: "rd", 49 | roomInvite: "i", 50 | revokeInvite: "ri", 51 | chatMessage: "c", 52 | privateMessage: "p", 53 | voiceStream: "v", 54 | changeStatus: "sc", 55 | customAction: "a", 56 | requestFriend: "f", 57 | acceptFriend: "fa", 58 | declineFriend: "fd", 59 | removeFriend: "fr", 60 | setUserVariable: "vs", 61 | setUserVariables: "vx" 62 | }; 63 | this.messageTypes = { 64 | CHAT: 0, 65 | PRIVATE: 1, 66 | SERVER: 2 67 | }; 68 | this.serverMessageTypes = { 69 | GAME: 0, 70 | NOTICE: 1, 71 | IMPORTANT: 2 72 | }; 73 | this.serverMessageNames = [ 74 | "Game", 75 | "Notice", 76 | "Important" 77 | ]; 78 | this.userStatuses = { 79 | available: 0, 80 | inGame: 1, 81 | idle: 2, 82 | offline: 3 83 | }; 84 | this.userStatusDefs = [ 85 | "Available", 86 | "In Game", 87 | "Idle", 88 | "Offline" 89 | ]; 90 | this.friendStatusDefs = { 91 | requested: 0, 92 | pending: 1, 93 | accepted: 2 94 | }; 95 | 96 | // EVENT DEFINITIONS 97 | this.events = { 98 | signup: "onsignup", 99 | accountDelete: "onaccountdelete", 100 | passwordChange: "onpasswordchange", 101 | accountInfoChange: "onaccountinfochange", 102 | autologInit: "onautologinit", 103 | autologFailed: "onautologfailed", 104 | autologNoFile: "onautolognofile", 105 | login: "onlogin", 106 | logout: "onlogout", 107 | connected: "onconnect", 108 | disconnected: "ondisconnect", 109 | joined: "onjoinroom", 110 | left: "onleaveroom", 111 | userJoined: "onuserjoin", 112 | userLeft: "onuserleft", 113 | roomCreate: "oncreateroom", 114 | roomDelete: "ondeleteroom", 115 | invited: "oninvite", 116 | inviteRevoked: "onrevokeinvite", 117 | inviteReceived: "oninvitereceived", 118 | chatMessage: "onchatmessage", 119 | privateMessage: "onprivatemessage", 120 | serverMessage: "onservermessage", 121 | data: "ondata", 122 | statusChanged: "onstatuschanged", // WHEN YOU CHANGE YOUR STATUS 123 | customAction: "oncustomaction", 124 | friendRequested: "onfriendrequested", // WHEN YOU REQUEST A FRIEND 125 | friendAccepted: "onfriendaccepted", // WHEN YOU ACCEPT A REQUEST 126 | friendDeclined: "onfrienddecline", // WHEN YOU DECLINE A REQUEST 127 | friendRemoved: "onfriendremove", // WHEN A FRIEND GETS REMOVED OR WHEN A USER DECLINES YOUR REQUEST 128 | friendRequestReceived: "onfriendrequestreceived", // WHEN YOU RECEIVE A FRIEND REQUEST FROM ANOTHER USER 129 | friendRequestAccepted: "onfriendrequestaccepted", // WHEN YOUR REQUEST TO ANOTHER USER IS ACCEPTED 130 | friendStatusChanged: "onfriendstatuschanged" // WHEN A FRIEND'S STATUS CHANGES 131 | }; 132 | 133 | // EVENT LISTENERS 134 | this.onSignupListener = null; 135 | this.onAccountDeleteListener = null; 136 | this.onPasswordChangeListener = null; 137 | this.onAccountInfoChangeListener = null; 138 | this.onAutoLogInitListener = null; 139 | this.onAutoLogFailListener = null; 140 | this.onAutoLogNoFileListener = null; 141 | this.onLoginListener = null; 142 | this.onLogoutListener = null; 143 | this.onConnectListener = null; 144 | this.onDisconnectListener = null; 145 | this.onJoinRoomListener = null; 146 | this.onLeaveRoomListener = null; 147 | this.onUserJoinListener = null; 148 | this.onUserLeaveListener = null; 149 | this.onCreateRoomListener = null; 150 | this.onDeleteRoomListener = null; 151 | this.onInviteListener = null; 152 | this.onRevokeListener = null; 153 | this.onReceiveInviteListener = null; 154 | this.onChatMsgListener = null; 155 | this.onPrivateMsgListener = null; 156 | this.onServerMsgListener = null; 157 | this.onDataListener = null; 158 | this.onStatusChangeListener = null; 159 | this.onCustomActionListener = null; 160 | this.onRequestFriendListener = null; 161 | this.onAcceptFriendListener = null; 162 | this.onDeclineFriendListener = null; 163 | this.onRemoveFriendListener = null; 164 | this.onFriendRequestReceivedListener = null; 165 | this.onFriendRequestAcceptedListener = null; 166 | this.onFriendStatusChangeListener = null; 167 | 168 | //ERROR MESSAGES 169 | this.paramError = "An incorrect parameter type was supplied" 170 | 171 | // 172 | return true; 173 | } 174 | 175 | GopherServerClient.prototype.connect = function(ip, port, ssl){ 176 | if(ip.constructor != String || port.constructor != Number || ssl.constructor != Boolean){ 177 | return this.paramError; 178 | } 179 | 180 | //SET CONFIG 181 | this.ip = ip; 182 | this.port = port; 183 | this.ssl = ssl; 184 | if(ssl == true){ 185 | this.socketURL = "wss://"+ip+":"+port+"/wss"; 186 | }else{ 187 | this.socketURL = "ws://"+ip+":"+port+"/ws"; 188 | } 189 | 190 | //START WEBSOCKET 191 | this.socket = new WebSocket(this.socketURL); 192 | this.socket.addEventListener("open", this.sO); 193 | this.socket.addEventListener("close", this.sD); 194 | this.socket.addEventListener("message", this.sR); 195 | } 196 | 197 | GopherServerClient.prototype.disconnect = function(){ 198 | this.socket.close(); 199 | } 200 | 201 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 202 | // SOCKET LISTENERS ////////////////////////////////////////////////////////////////////////////// 203 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 204 | 205 | GopherServerClient.prototype.sO = function(e){ 206 | var self = gopherClient; 207 | 208 | // 209 | self.connected = true; 210 | 211 | // 212 | if(self.onConnectListener != null){ 213 | self.onConnectListener(); 214 | } 215 | } 216 | 217 | GopherServerClient.prototype.sD = function(e){ 218 | var self = gopherClient; 219 | 220 | // 221 | self.socket.removeEventListener("open", self.sO); 222 | self.socket.removeEventListener("close", self.sD); 223 | self.socket.removeEventListener("message", self.sR); 224 | 225 | //RESET VARIABLES 226 | self.connected = false; 227 | self.loggedIn = false; 228 | self.rememberMe = false; 229 | self.guest = false; 230 | self.userName = ""; 231 | self.roomName = ""; 232 | self.status = 0; 233 | self.friends = {}; 234 | 235 | //CALL THE DISCONNECT LISTENER 236 | if(self.onDisconnectListener != null){ 237 | self.onDisconnectListener(e); 238 | } 239 | } 240 | 241 | GopherServerClient.prototype.sR = function(e){ 242 | var data = JSON.parse(e.data); 243 | gopherClient.sRhandle(data); 244 | } 245 | 246 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 247 | // GOPHER EVENT LISTENERS //////////////////////////////////////////////////////////////////////// 248 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 249 | 250 | GopherServerClient.prototype.addEventListener = function(type, callback){ 251 | if(type.constructor != String || callback.constructor != Function){ 252 | return this.paramError + " (addEventListener: " + type + ", " + callback + ")"; 253 | } 254 | switch (type) { 255 | case this.events.signup: this.onSignupListener = callback; break; 256 | case this.events.passwordChange: this.onPasswordChangeListener = callback; break; 257 | case this.events.accountInfoChange: this.onAccountInfoChangeListener = callback; break; 258 | case this.events.accountDelete: this.onAccountDeleteListener = callback; break; 259 | case this.events.login: this.onLoginListener = callback; break; 260 | case this.events.logout: this.onLogoutListener = callback; break; 261 | case this.events.connected: this.onConnectListener = callback; break; 262 | case this.events.disconnected: this.onDisconnectListener = callback; break; 263 | case this.events.joined: this.onJoinRoomListener = callback; break; 264 | case this.events.left: this.onLeaveRoomListener = callback; break; 265 | case this.events.userJoined: this.onUserJoinListener = callback; break; 266 | case this.events.userLeft: this.onUserLeaveListener = callback; break; 267 | case this.events.roomCreate: this.onCreateRoomListener = callback; break; 268 | case this.events.roomDelete: this.onDeleteRoomListener = callback; break; 269 | case this.events.invited: this.onInviteListener = callback; break; 270 | case this.events.inviteRevoked: this.onRevokeListener = callback; break; 271 | case this.events.inviteReceived: this.onReceiveInviteListener = callback; break; 272 | case this.events.chatMessage: this.onChatMsgListener = callback; break; 273 | case this.events.privateMessage: this.onPrivateMsgListener = callback; break; 274 | case this.events.serverMessage: this.onServerMsgListener = callback; break; 275 | case this.events.data: this.onDataListener = callback; break; 276 | case this.events.statusChanged: this.onStatusChangeListener = callback; break; 277 | case this.events.customAction: this.onCustomActionListener = callback; break; 278 | case this.events.friendRequested: this.onRequestFriendListener = callback; break; 279 | case this.events.friendAccepted: this.onAcceptFriendListener = callback; break; 280 | case this.events.friendDeclined: this.onDeclineFriendListener = callback; break; 281 | case this.events.friendRemoved: this.onRemoveFriendListener = callback; break; 282 | case this.events.friendRequestReceived: this.onFriendRequestReceivedListener = callback; break; 283 | case this.events.friendRequestAccepted: this.onFriendRequestAcceptedListener = callback; break; 284 | case this.events.friendStatusChanged: this.onFriendStatusChangeListener = callback; break; 285 | case this.events.autologInit: this.onAutoLogInitListener = callback; break; 286 | case this.events.autologFailed: this.onAutoLogFailListener = callback; break; 287 | case this.events.autologNoFile: this.onAutoLogNoFileListener = callback; break; 288 | default: console.log("Unrecognized gopherClient event: '" + type + "'"); 289 | } 290 | } 291 | 292 | GopherServerClient.prototype.removeEventListener = function(type){ 293 | if(type.constructor != String){ 294 | return this.paramError + " (removeEventListener: " + type + ")"; 295 | } 296 | switch (type) { 297 | case this.events.signup: this.onSignupListener = null; break; 298 | case this.events.passwordChange: this.onPasswordChangeListener = null; break; 299 | case this.events.accountInfoChange: this.onAccountInfoChangeListener = null; break; 300 | case this.events.accountDelete: this.onAccountDeleteListener = null; break; 301 | case this.events.login: this.onLoginListener = null; break; 302 | case this.events.logout: this.onLogoutListener = null; break; 303 | case this.events.connected: this.onConnectListener = null; break; 304 | case this.events.disconnected: this.onDisconnectListener = null; break; 305 | case this.events.joined: this.onJoinRoomListener = null; break; 306 | case this.events.left: this.onLeaveRoomListener = null; break; 307 | case this.events.userJoined: this.onUserJoinListener = null; break; 308 | case this.events.userLeft: this.onUserLeaveListener = null; break; 309 | case this.events.roomCreate: this.onCreateRoomListener = null; break; 310 | case this.events.roomDelete: this.onDeleteRoomListener = null; break; 311 | case this.events.invited: this.onInviteListener = null; break; 312 | case this.events.inviteRevoked: this.onRevokeListener = null; break; 313 | case this.events.inviteReceived: this.onReceiveInviteListener = null; break; 314 | case this.events.chatMessage: this.onChatMsgListener = null; break; 315 | case this.events.privateMessage: this.onPrivateMsgListener = null; break; 316 | case this.events.serverMessage: this.onServerMsgListener = null; break; 317 | case this.events.data: this.onDataListener = null; break; 318 | case this.events.statusChanged: this.onStatusChangeListener = null; break; 319 | case this.events.customAction: this.onCustomActionListener = null; break; 320 | case this.events.friendRequested: this.onRequestFriendListener = null; break; 321 | case this.events.friendAccepted: this.onAcceptFriendListener = null; break; 322 | case this.events.friendDeclined: this.onDeclineFriendListener = null; break; 323 | case this.events.friendRemoved: this.onRemoveFriendListener = null; break; 324 | case this.events.friendRequestReceived: this.onFriendRequestReceivedListener = null; break; 325 | case this.events.friendRequestAccepted: this.onFriendRequestAcceptedListener = null; break; 326 | case this.events.friendStatusChanged: this.onFriendStatusChangeListener = null; break; 327 | case this.events.autologInit: this.onAutoLogInitListener = null; break; 328 | case this.events.autologFailed: this.onAutoLogFailListener = null; break; 329 | case this.events.autologNoFile: this.onAutoLogNoFileListener = null; break; 330 | default: console.log("Unrecognized gopherClient event: '" + type + "'"); 331 | } 332 | } 333 | 334 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 335 | // SOCKET MESSAGE HANDLER //////////////////////////////////////////////////////////////////////// 336 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 337 | 338 | GopherServerClient.prototype.sRhandle = function(data){ 339 | if(data.v !== undefined){ 340 | //VOICE STREAM (highest look-up priority) 341 | this.voiceChat.rD(data.v); 342 | }if(data.vp !== undefined){ 343 | //VOICE PING (high look-up priority) 344 | this.voiceChat.pD(); 345 | }else if(data.d !== undefined){ 346 | //RECEIVED DATA (high look-up priority) 347 | if(this.onDataListener != null){ 348 | this.onDataListener(data.d); 349 | } 350 | }else if(data.a !== undefined){ 351 | //CUSTOM CLIENT ACTION RESPONSE 352 | this.customClientActionResponse(data.a); 353 | }else if(data.c !== undefined){ 354 | //BUILT-IN CLIENT ACTION RESPONSES 355 | switch (data.c.a) { 356 | case this.clientActionDefs.setUserVariable: this.setUserVariableResponse(data.c); break; 357 | case this.clientActionDefs.setUserVariables: this.setUserVariablesResponse(data.c); break; 358 | case this.clientActionDefs.signup: this.signupResponse(data.c); break; 359 | case this.clientActionDefs.deleteAccount: this.deleteAccountResponse(data.c); break; 360 | case this.clientActionDefs.changeAccountInfo: this.changeAccountInfoResponse(data.c); break; 361 | case this.clientActionDefs.changePassword: this.changePasswordResponse(data.c); break; 362 | case this.clientActionDefs.login: this.loginReponse(data.c); break; 363 | case this.clientActionDefs.logout: this.logoutReponse(data.c); break; 364 | case this.clientActionDefs.joinRoom: this.joinRoomResponse(data.c); break; 365 | case this.clientActionDefs.leaveRoom: this.leaveRoomResponse(data.c); break; 366 | case this.clientActionDefs.createRoom: this.createRoomResponse(data.c); break; 367 | case this.clientActionDefs.roomInvite: this.sendInviteResponse(data.c); break; 368 | case this.clientActionDefs.revokeInvite: this.revokeInviteResponse(data.c); break; 369 | case this.clientActionDefs.deleteRoom: this.deleteRoomResponse(data.c); break; 370 | case this.clientActionDefs.requestFriend: this.requestFriendResponse(data.c); break; 371 | case this.clientActionDefs.acceptFriend: this.acceptFriendResponse(data.c); break; 372 | case this.clientActionDefs.declineFriend: this.declineFriendResponse(data.c); break; 373 | case this.clientActionDefs.removeFriend: this.removeFriendResponse(data.c); break; 374 | case this.clientActionDefs.changeStatus: this.changeStatusResponse(data.c); break; 375 | default: console.log("Unrecognized client action response '" + data.c.a + "': " + data.c); 376 | } 377 | }else if(data.e !== undefined){ 378 | //USER ENTERED ROOM 379 | if(this.onUserJoinListener != null){ 380 | this.onUserJoinListener(data.e.u, data.e.g); // userName, isGuest 381 | } 382 | }else if(data.x !== undefined){ 383 | //USER EXITED ROOM 384 | if(this.onUserLeaveListener != null){ 385 | this.onUserLeaveListener(data.x.u); // userName 386 | } 387 | }else if(data.m !== undefined){ 388 | //RECEIVED ROOM MESSAGE 389 | if(data.m.s !== undefined){ 390 | //TYPE SERVER 391 | if(this.onServerMsgListener != null){ 392 | this.onServerMsgListener(data.m.s, data.m.m) // sub-type, message 393 | } 394 | }else{ 395 | //TYPE CHAT 396 | if(this.onChatMsgListener != null){ 397 | this.onChatMsgListener(data.m.a, data.m.m); // author, message 398 | } 399 | } 400 | }else if(data.p !== undefined){ 401 | //RECEIVED PRIVATE MESSAGE 402 | if(this.onPrivateMsgListener != null){ 403 | this.onPrivateMsgListener(data.p.f, data.p.t, data.p.m); // from, to, message 404 | } 405 | }else if(data.i !== undefined){ 406 | //RECEIVED INVITATION TO ROOM 407 | if(this.onReceiveInviteListener != null){ 408 | this.onReceiveInviteListener(data.i.u, data.i.r); // userName, roomName 409 | } 410 | }else if(data.f !== undefined){ 411 | //RECEIVED FRIEND REQUEST 412 | this.friends[data.f.n] = {name: data.f.n, requestStatus: this.friendStatusDefs.requested, status: -1}; 413 | if(this.onFriendRequestReceivedListener != null){ 414 | this.onFriendRequestReceivedListener(data.f.n); // userName 415 | } 416 | }else if(data.fa !== undefined){ 417 | //FRIEND REQUEST WAS ACCEPTED 418 | if(this.friends[data.fa.n] != undefined){ 419 | this.friends[data.fa.n].requestStatus = this.friendStatusDefs.accepted; 420 | this.friends[data.fa.n].status = data.fa.s; 421 | }else{ 422 | this.friends[data.fa.n] = {name: data.fa.n, requestStatus: this.friendStatusDefs.accepted, status: data.fa.s}; 423 | } 424 | if(this.onFriendRequestAcceptedListener != null){ 425 | this.onFriendRequestAcceptedListener(data.fa.n); // userName 426 | } 427 | }else if(data.fr !== undefined){ 428 | //FRIEND WAS REMOVED 429 | if(this.friends[data.fr.n] != undefined){ 430 | delete this.friends[data.fr.n]; 431 | } 432 | if(this.onRemoveFriendListener != null){ 433 | this.onRemoveFriendListener(data.fr.n); // userName 434 | } 435 | }else if(data.fs !== undefined){ 436 | //FRIEND'S STATUS CHANGED 437 | if(this.friends[data.fs.n] != undefined){ 438 | this.friends[data.fs.n].status = data.fs.s; 439 | }else{ 440 | this.friends[data.fs.n] = {name: data.fs.n, requestStatus: this.friendStatusDefs.accepted, status: data.fs.s}; 441 | } 442 | if(this.onFriendStatusChangeListener != null){ 443 | this.onFriendStatusChangeListener(data.fs.n, data.fs.s); // userName, status 444 | } 445 | }else if(data.t !== undefined){ 446 | //SERVER REQUESTED AUTO-LOG INFO 447 | if(this.onAutoLogInitListener != null){ 448 | this.onAutoLogInitListener(); 449 | } 450 | if(localStorage.getItem('dt')){ 451 | if(localStorage.getItem('da') && localStorage.getItem('di')){ 452 | this.socket.send(JSON.stringify({A: "2", P: {dt: localStorage.getItem('dt'), da: localStorage.getItem('da'), 453 | di: localStorage.getItem('di')}})); 454 | }else{ 455 | this.socket.send(JSON.stringify({A: "1", P: localStorage.getItem('dt')})); 456 | } 457 | }else{ 458 | this.socket.send(JSON.stringify({A: "0", P: null})); 459 | } 460 | }else if(data.ts !== undefined){ 461 | //SERVER WANTS TO SET DEVICE TAG 462 | localStorage.setItem('dt', data.ts); 463 | this.socket.send(JSON.stringify({A: "1", P: localStorage.getItem('dt')})); 464 | }else if(data.ap !== undefined){ 465 | //SERVER WANTS TO SET DEVICE PASS 466 | this.socket.send(JSON.stringify({A: "3", P: null})); 467 | localStorage.setItem('da', data.ap); 468 | }else if(data.af !== undefined){ //// REMEMBER, AUTO-LOGIN TRIGGERS onLoginListener IF SUCCESSFUL. 469 | //AUTO-LOGIN FAILED 470 | localStorage.clear(); 471 | localStorage.setItem('dt', data.af.dt); 472 | if(this.onAutoLogFailListener != null){ 473 | this.onAutoLogFailListener(data.af.e); 474 | } 475 | }else if(data.ai !== undefined){ 476 | //AUTO-LOGIN NOT FILED 477 | if(this.onAutoLogNoFileListener != null){ 478 | this.onAutoLogNoFileListener(); 479 | } 480 | } 481 | } 482 | 483 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 484 | // BUILT-IN CLIENT ACTION FUNCTIONS/HANDLERS ///////////////////////////////////////////////////// 485 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 486 | 487 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 488 | // ACCOUNT ACTIONS /////////////////////////////////////////////////////////////////////////////// 489 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 490 | 491 | // SIGN UP ////////////////////////////////////////////////// 492 | 493 | GopherServerClient.prototype.signup = function(userName, password, customCols){ 494 | if(userName == null || password == null || userName.constructor != String || 495 | password.constructor != String || (customCols != null && customCols.constructor != Object)){ 496 | return this.paramError; 497 | } 498 | this.socket.send(JSON.stringify({A: this.clientActionDefs.signup, P: {n: userName, p: password, c:customCols}})); 499 | } 500 | 501 | GopherServerClient.prototype.signupResponse = function(data){ 502 | if(data.e !== undefined){ 503 | if(this.onSignupListener != null){ 504 | this.onSignupListener(false, data.e); 505 | } 506 | }else{ 507 | if(this.onSignupListener != null){ 508 | this.onSignupListener(true, null); 509 | } 510 | } 511 | } 512 | 513 | // DELETE AN ACCOUNT ////////////////////////////////////////////////// 514 | 515 | GopherServerClient.prototype.deleteAccount = function(userName, password, customCols){ 516 | if(userName == null || password == null || userName.constructor != String || 517 | password.constructor != String || (customCols != null && customCols.constructor != Object)){ 518 | return this.paramError; 519 | } 520 | this.socket.send(JSON.stringify({A: this.clientActionDefs.deleteAccount, P: {n: userName, p: password, c:customCols}})); 521 | } 522 | 523 | GopherServerClient.prototype.deleteAccountResponse = function(data){ 524 | if(data.e !== undefined){ 525 | if(this.onAccountDeleteListener != null){ 526 | this.onAccountDeleteListener(false, data.e); 527 | } 528 | }else{ 529 | if(this.onAccountDeleteListener != null){ 530 | this.onAccountDeleteListener(true, null); 531 | } 532 | } 533 | } 534 | 535 | // CHANGE ACCOUNT INFO ////////////////////////////////////////////////// 536 | 537 | GopherServerClient.prototype.changeAccountInfo = function(password, customCols){ 538 | if(!this.loggedIn){ 539 | return "You must be logged in to change your account info"; 540 | }else if(password == null || password.constructor != String || customCols == null || customCols.constructor != Object){ 541 | return this.paramError; 542 | } 543 | this.socket.send(JSON.stringify({A: this.clientActionDefs.changeAccountInfo, P: {p: password, c:customCols}})); 544 | } 545 | 546 | GopherServerClient.prototype.changeAccountInfoResponse = function(data){ 547 | if(data.e !== undefined){ 548 | if(this.onAccountInfoChangeListener != null){ 549 | this.onAccountInfoChangeListener(false, data.e); 550 | } 551 | }else{ 552 | if(this.onAccountInfoChangeListener != null){ 553 | this.onAccountInfoChangeListener(true, null); 554 | } 555 | } 556 | } 557 | 558 | // CHANGE PASSWORD ////////////////////////////////////////////////// 559 | 560 | GopherServerClient.prototype.changePassword = function(password, newPassword, customCols){ 561 | if(!this.loggedIn){ 562 | return "You must be logged in to change your password"; 563 | } 564 | if(password == null || newPassword == null || password.constructor != String || 565 | newPassword.constructor != String || (customCols != null && customCols.constructor != Object)){ 566 | return this.paramError; 567 | } 568 | this.socket.send(JSON.stringify({A: this.clientActionDefs.changePassword, P: {p: password, n: newPassword, c:customCols}})); 569 | } 570 | 571 | GopherServerClient.prototype.changePasswordResponse = function(data){ 572 | if(data.e !== undefined){ 573 | if(this.onPasswordChangeListener != null){ 574 | this.onPasswordChangeListener(false, data.e); 575 | } 576 | }else{ 577 | if(this.onPasswordChangeListener != null){ 578 | this.onPasswordChangeListener(true, null); 579 | } 580 | } 581 | } 582 | 583 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 584 | // LOGIN/LOGOUT ACTIONS ////////////////////////////////////////////////////////////////////////// 585 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 586 | 587 | // LOG IN ////////////////////////////////////////////////// 588 | 589 | GopherServerClient.prototype.login = function(userName, password, rememberMe, isGuest, customCols){ 590 | if(userName == null || userName.constructor != String || (isGuest != null && isGuest.constructor != Boolean) || (rememberMe != null && rememberMe.constructor != Boolean) 591 | || (password != null && password.constructor != String) || (customCols != null && customCols.constructor != Object)){ 592 | return this.paramError; 593 | } 594 | this.socket.send(JSON.stringify({A: this.clientActionDefs.login, P: {n: userName, p: password, g: isGuest, r: rememberMe, c: customCols}})); 595 | this.guest = isGuest; 596 | if(rememberMe == null){ 597 | this.rememberMe = false; 598 | }else{ 599 | this.rememberMe = rememberMe; 600 | } 601 | } 602 | 603 | GopherServerClient.prototype.loginReponse = function(data){ 604 | if(data.e !== undefined){ 605 | this.guest = false; 606 | if(this.onLoginListener != null){ 607 | this.onLoginListener("", data.e); 608 | } 609 | }else{ 610 | this.userName = data.r.n; 611 | this.loggedIn = true; 612 | //MAKE FRIENDS 613 | var fList = data.r.f; 614 | if(fList != undefined && fList != null){ 615 | for(var i = 0; i < fList.length; i++){ 616 | var status = -1; 617 | if(fList[i]["s"] != undefined){ 618 | status = fList[i]["s"]; 619 | } 620 | this.friends[fList[i]["n"]] = {name: fList[i]["n"], requestStatus: fList[i]["rs"], status: status}; 621 | } 622 | } 623 | //SET AUTO-LOG IF PROVIDED 624 | if(data.r.ap && this.rememberMe){ 625 | localStorage.setItem('da', data.r.ap); 626 | localStorage.setItem('di', data.r.ai); 627 | } 628 | // 629 | if(this.onLoginListener != null){ 630 | this.onLoginListener(this.userName, null); 631 | } 632 | } 633 | } 634 | 635 | // LOG OUT ////////////////////////////////////////////////// 636 | 637 | GopherServerClient.prototype.logout = function(){ 638 | this.socket.send(JSON.stringify({A: this.clientActionDefs.logout})); 639 | } 640 | 641 | GopherServerClient.prototype.logoutReponse = function(data){ 642 | if(data.e !== undefined){ 643 | if(this.onLogoutListener != null){ 644 | this.onLogoutListener(false, data.e); 645 | } 646 | }else{ 647 | this.userName = ""; 648 | this.loggedIn = false; 649 | this.guest = false; 650 | this.roomName = ""; 651 | this.status = 0; 652 | this.friends = {}; 653 | //UN-SET AUTO-LOG IF SET 654 | if(localStorage.getItem('da')){ 655 | localStorage.removeItem("da"); 656 | localStorage.removeItem("di"); 657 | } 658 | // 659 | if(this.onLogoutListener != null){ 660 | this.onLogoutListener(true, null); 661 | } 662 | } 663 | } 664 | 665 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 666 | // ROOM ACTIONS ////////////////////////////////////////////////////////////////////////////////// 667 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 668 | 669 | // JOIN ROOM ////////////////////////////////////////////////// 670 | 671 | GopherServerClient.prototype.joinRoom = function(roomName){ 672 | if(roomName == null || roomName.constructor != String){ 673 | return this.paramError; 674 | } 675 | this.roomName = roomName; 676 | this.socket.send(JSON.stringify({A: this.clientActionDefs.joinRoom, P: roomName})); 677 | } 678 | 679 | GopherServerClient.prototype.joinRoomResponse = function(data){ 680 | if(data.e !== undefined){ 681 | this.roomName = ""; 682 | if(this.onJoinRoomListener != null){ 683 | this.onJoinRoomListener("", data.e); 684 | } 685 | }else{ 686 | if(this.onJoinRoomListener != null && this.onJoinRoomListener != null){ 687 | this.onJoinRoomListener(data.r, null); 688 | } 689 | } 690 | } 691 | 692 | // LEAVE ROOM ////////////////////////////////////////////////// 693 | 694 | GopherServerClient.prototype.leaveRoom = function(){ 695 | this.socket.send(JSON.stringify({A: this.clientActionDefs.leaveRoom})); 696 | } 697 | 698 | GopherServerClient.prototype.leaveRoomResponse = function(data){ 699 | if(data.e !== undefined){ 700 | if(this.onLeaveRoomListener != null){ 701 | this.onLeaveRoomListener("", data.e); 702 | } 703 | }else{ 704 | var tempRoom = this.roomName; 705 | this.roomName = ""; 706 | // 707 | if(this.onLeaveRoomListener != null){ 708 | this.onLeaveRoomListener(tempRoom, null); 709 | } 710 | } 711 | } 712 | 713 | // CREATE A ROOM ////////////////////////////////////////////////// 714 | 715 | GopherServerClient.prototype.createRoom = function(roomName, roomType, isPrivate, maxUsers){ 716 | if(roomName == null || roomType == null || isPrivate == null || maxUsers == null || 717 | roomName.constructor != String || roomType.constructor != String || isPrivate.constructor != Boolean || maxUsers.constructor != Number){ 718 | return this.paramError; 719 | } 720 | this.socket.send(JSON.stringify({A: this.clientActionDefs.createRoom, P: {n: roomName, t: roomType, p: isPrivate, m: Math.round(maxUsers)}})); 721 | } 722 | 723 | GopherServerClient.prototype.createRoomResponse = function(data){ 724 | if(data.e !== undefined){ 725 | if(this.onCreateRoomListener != null){ 726 | this.onCreateRoomListener("", data.e); 727 | } 728 | }else{ 729 | //THE ROOM YOU MADE. MAKING A ROOM AUTO-JOINS THE CLIENT INTO IT. 730 | this.roomName = data.r; 731 | // 732 | if(this.onCreateRoomListener != null){ 733 | this.onCreateRoomListener(data.r, null); 734 | } 735 | } 736 | } 737 | 738 | // DELETE A ROOM ////////////////////////////////////////////////// 739 | 740 | GopherServerClient.prototype.deleteRoom = function(roomName){ 741 | if(roomName == null || roomName.constructor != String){ 742 | return this.paramError; 743 | } 744 | this.socket.send(JSON.stringify({A: this.clientActionDefs.deleteRoom, P: roomName})); 745 | } 746 | 747 | GopherServerClient.prototype.deleteRoomResponse = function(data){ 748 | if(data.e !== undefined){ 749 | if(this.onDeleteRoomListener != null){ 750 | this.onDeleteRoomListener("", data.e); 751 | } 752 | }else{ 753 | if(this.onDeleteRoomListener != null){ 754 | this.onDeleteRoomListener(data.r, null); 755 | } 756 | } 757 | } 758 | 759 | // INVITE TO ROOM ////////////////////////////////////////////////// 760 | 761 | GopherServerClient.prototype.sendInvite = function(userName){ 762 | if(userName == null || userName.constructor != String){ 763 | return this.paramError; 764 | } 765 | this.socket.send(JSON.stringify({A: this.clientActionDefs.roomInvite, P: userName})); 766 | } 767 | 768 | GopherServerClient.prototype.sendInviteResponse = function(data){ 769 | if(data.e !== undefined){ 770 | if(this.onInviteListener != null){ 771 | this.onInviteListener(false, data.e); 772 | } 773 | }else{ 774 | if(this.onInviteListener != null){ 775 | this.onInviteListener(true, null); 776 | } 777 | } 778 | } 779 | 780 | // REVOKE INVITE TO ROOM ////////////////////////////////////////////////// 781 | 782 | GopherServerClient.prototype.revokeInvite = function(userName){ 783 | if(userName == null || userName.constructor != String){ 784 | return this.paramError; 785 | } 786 | this.socket.send(JSON.stringify({A: this.clientActionDefs.revokeInvite, P: userName})); 787 | } 788 | 789 | GopherServerClient.prototype.revokeInviteResponse = function(data){ 790 | if(data.e !== undefined){ 791 | if(this.onRevokeListener != null){ 792 | this.onRevokeListener(false, data.e); 793 | } 794 | }else{ 795 | if(this.onRevokeListener != null){ 796 | this.onRevokeListener(true, null); 797 | } 798 | } 799 | } 800 | 801 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 802 | // MESSAGING ///////////////////////////////////////////////////////////////////////////////////// 803 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 804 | 805 | // CHAT MESSAGE ////////////////////////////////////////////////// 806 | 807 | GopherServerClient.prototype.chatMessage = function(message){ 808 | if(message == null || (message.constructor != String && message.constructor != Object && message.constructor != Array)){ 809 | return this.paramError; 810 | } 811 | this.socket.send(JSON.stringify({A: this.clientActionDefs.chatMessage, P: message})); 812 | } 813 | 814 | // PRIVATE MESSAGE ////////////////////////////////////////////////// 815 | 816 | GopherServerClient.prototype.privateMessage = function(userName, message){ 817 | if(message == null || userName == null || userName.constructor != String || 818 | (message.constructor != String && message.constructor != Object && message.constructor != Array)){ 819 | return this.paramError; 820 | } 821 | this.socket.send(JSON.stringify({A: this.clientActionDefs.privateMessage, P: {u: userName, m: message}})); 822 | } 823 | 824 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 825 | // CUSTOM ACTIONS //////////////////////////////////////////////////////////////////////////////// 826 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 827 | 828 | // CUSTOM CLIENT ACTION ////////////////////////////////////////////////// 829 | 830 | GopherServerClient.prototype.customClientAction = function(action, data){ 831 | if(action.constructor != String || (data.constructor != null && data.constructor != Boolean && data.constructor != Number 832 | && data.constructor != String && data.constructor != Array && data.constructor != Object)){ 833 | return this.paramError; 834 | } 835 | this.socket.send(JSON.stringify({A: this.clientActionDefs.customAction, P: {a: action, d: data}})); 836 | } 837 | 838 | GopherServerClient.prototype.customClientActionResponse = function(data){ 839 | if(data.e !== undefined){ 840 | if(this.onCustomActionListener != null){ 841 | this.onCustomActionListener(null, data.a, data.e); // responseData, actionType, error 842 | } 843 | }else{ 844 | if(this.onCustomActionListener != null){ 845 | this.onCustomActionListener(data.r, data.a, null); // responseData, actionType, error 846 | } 847 | } 848 | } 849 | 850 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 851 | // FRIENDING ACTIONS ///////////////////////////////////////////////////////////////////////////// 852 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 853 | 854 | // REQUEST A FRIEND ////////////////////////////////////////////////// 855 | 856 | GopherServerClient.prototype.requestFriend = function(friendName){ 857 | if(friendName == null || friendName.constructor != String){ 858 | return this.paramError; 859 | }else if(this.friends[friendName] != undefined){ 860 | return "You cannot request '"+friendName+"' as a friend at this time"; 861 | } 862 | this.socket.send(JSON.stringify({A: this.clientActionDefs.requestFriend, P: friendName})); 863 | } 864 | 865 | GopherServerClient.prototype.requestFriendResponse = function(data){ 866 | if(data.e !== undefined){ 867 | if(this.onRequestFriendListener != null){ 868 | this.onRequestFriendListener("", data.e); // friendName, error 869 | } 870 | }else{ 871 | //ADD FRIEND 872 | this.friends[data.r] = {name: data.r, requestStatus: this.friendStatusDefs.pending, status: -1}; 873 | // 874 | if(this.onRequestFriendListener != null){ 875 | this.onRequestFriendListener(data.r, null); // friendName, error 876 | } 877 | } 878 | } 879 | 880 | // ACCEPT A FRIEND REQUEST ////////////////////////////////////////////////// 881 | 882 | GopherServerClient.prototype.acceptFriend = function(friendName){ 883 | if(friendName == null || friendName.constructor != String){ 884 | return this.paramError; 885 | }else if(this.friends[friendName] == undefined){ 886 | return "No friend by the name '"+friendName+"'"; 887 | }else if(this.friends[friendName].requestStatus != this.friendStatusDefs.requested){ 888 | return "Cannot accept '"+friendName+"' as a friend"; 889 | } 890 | this.socket.send(JSON.stringify({A: this.clientActionDefs.acceptFriend, P: friendName})); 891 | } 892 | 893 | GopherServerClient.prototype.acceptFriendResponse = function(data){ 894 | if(data.e !== undefined){ 895 | if(this.onAcceptFriendListener != null){ 896 | this.onAcceptFriendListener("", data.e); // friendName, error 897 | } 898 | }else{ 899 | //UPDATE/ADD FRIEND 900 | if(this.friends[data.r.n] != undefined){ 901 | this.friends[data.r.n].requestStatus = this.friendStatusDefs.accepted; 902 | this.friends[data.r.n].status = data.r.s; 903 | }else{ 904 | this.friends[data.r.n] = {name: data.r.n, requestStatus: this.friendStatusDefs.accepted, status: data.r.s}; 905 | } 906 | // 907 | if(this.onAcceptFriendListener != null){ 908 | this.onAcceptFriendListener(data.r.n, null); // friendName, error 909 | } 910 | } 911 | } 912 | 913 | // DECLINE A FRIEND REQUEST ////////////////////////////////////////////////// 914 | 915 | GopherServerClient.prototype.declineFriend = function(friendName){ 916 | if(friendName == null || friendName.constructor != String){ 917 | return this.paramError; 918 | }else if(this.friends[friendName] == undefined){ 919 | return "No friend by the name '"+friendName+"'"; 920 | }else if(this.friends[friendName].requestStatus != this.friendStatusDefs.requested){ 921 | return "Cannot decline '"+friendName+"' as a friend"; 922 | } 923 | this.socket.send(JSON.stringify({A: this.clientActionDefs.declineFriend, P: friendName})); 924 | } 925 | 926 | GopherServerClient.prototype.declineFriendResponse = function(data){ 927 | if(data.e !== undefined){ 928 | if(this.onDeclineFriendListener != null){ 929 | this.onDeclineFriendListener("", data.e); // friendName, error 930 | } 931 | }else{ 932 | // 933 | if(this.onDeclineFriendListener != null){ 934 | this.onDeclineFriendListener(data.r, null); // friendName, error 935 | } 936 | //REMOVE FRIEND 937 | if(this.friends[data.r] != undefined){ 938 | delete this.friends[data.r]; 939 | } 940 | } 941 | } 942 | 943 | // REMOVE A FRIEND ////////////////////////////////////////////////// 944 | 945 | GopherServerClient.prototype.removeFriend = function(friendName){ 946 | if(friendName == null || friendName.constructor != String){ 947 | return this.paramError; 948 | }else if(this.friends[friendName] == undefined){ 949 | return "No friend by the name '"+friendName+"'"; 950 | } 951 | this.socket.send(JSON.stringify({A: this.clientActionDefs.removeFriend, P: friendName})); 952 | } 953 | 954 | GopherServerClient.prototype.removeFriendResponse = function(data){ 955 | if(data.e !== undefined){ 956 | if(this.onRemoveFriendListener != null){ 957 | this.onRemoveFriendListener("", data.e); // friendName, error 958 | } 959 | }else{ 960 | // 961 | if(this.onRemoveFriendListener != null){ 962 | this.onRemoveFriendListener(data.r, null); // friendName, error 963 | } 964 | //REMOVE FRIEND 965 | if(this.friends[data.r] != undefined){ 966 | delete this.friends[data.r]; 967 | } 968 | } 969 | } 970 | 971 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 972 | // CHANGING STATUS /////////////////////////////////////////////////////////////////////////////// 973 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 974 | 975 | // CHANGE YOUR STATUS ////////////////////////////////////////////////// 976 | 977 | GopherServerClient.prototype.changeStatus = function(status){ 978 | if(status == null || status.constructor != Number || status < 0){ 979 | return this.paramError; 980 | } 981 | this.socket.send(JSON.stringify({A: this.clientActionDefs.changeStatus, P: Math.round(status)})); 982 | } 983 | 984 | GopherServerClient.prototype.changeStatusResponse = function(data){ 985 | if(data.e !== undefined){ 986 | if(this.onStatusChangeListener != null){ 987 | this.onStatusChangeListener(0, data.e); // status, error 988 | } 989 | }else{ 990 | //CHANGE STATUS 991 | this.status = data.r; 992 | // 993 | if(this.onStatusChangeListener != null){ 994 | this.onStatusChangeListener(data.r, null); // status, error 995 | } 996 | } 997 | } 998 | 999 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 1000 | // USER VARIABLES //////////////////////////////////////////////////////////////////////////////// 1001 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 1002 | 1003 | // SET USER VARIABLE ////////////////////////////////////////////////// 1004 | 1005 | GopherServerClient.prototype.setUserVariable = function(key, value){ 1006 | if(key == undefined || key.constructor != String){ 1007 | return this.paramError; 1008 | } 1009 | this.socket.send(JSON.stringify({A: this.clientActionDefs.setUserVariable, P: {k:key, v: value}})); 1010 | } 1011 | 1012 | GopherServerClient.prototype.setUserVariableResponse = function(data){ 1013 | this.userVars[data.r.k] = data.r.v; 1014 | } 1015 | 1016 | // SET MULTIPLE USER VARIABLES ////////////////////////////////////////////////// 1017 | 1018 | GopherServerClient.prototype.setUserVariables = function(values){ 1019 | if(values == null || values.constructor != Object){ 1020 | return this.paramError; 1021 | } 1022 | this.socket.send(JSON.stringify({A: this.clientActionDefs.setUserVariables, P: values})); 1023 | } 1024 | 1025 | GopherServerClient.prototype.setUserVariablesResponse = function(data){ 1026 | var keys = Object.keys(data.r); 1027 | for(var i = 0; i < keys.length; i++){ 1028 | this.userVars[keys[i]] = data.r[keys[i]]; 1029 | } 1030 | } 1031 | 1032 | // USER VARIABLE GETTER ////////////////////////////////////////////////// 1033 | 1034 | GopherServerClient.prototype.getUserVariable = function(key){ 1035 | return this.userVars[key]; 1036 | } 1037 | 1038 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 1039 | // OBJECT GETTERS //////////////////////////////////////////////////////////////////////////////// 1040 | ////////////////////////////////////////////////////////////////////////////////////////////////////// 1041 | 1042 | // isConnected returns a boolean that's true if the client is connected 1043 | GopherServerClient.prototype.isConnected = function(){ 1044 | return this.connected; 1045 | } 1046 | 1047 | // isLoggedIn returns a boolean that's true if the client is logged in as a User 1048 | GopherServerClient.prototype.isLoggedIn = function(){ 1049 | return this.loggedIn; 1050 | } 1051 | 1052 | // isGuest returns a boolean that's true if the client is a guest 1053 | GopherServerClient.prototype.isGuest = function(){ 1054 | return this.guest; 1055 | } 1056 | 1057 | // getUserName returns the name of the User the client is currently logged in as. A blank string is returned 1058 | // if the client is not logged in. 1059 | GopherServerClient.prototype.getUserName = function(){ 1060 | return this.userName; 1061 | } 1062 | 1063 | // getRoom returns the name of the Room the client User is currently in. A blank string is returned 1064 | // if the client User is not in a room. 1065 | GopherServerClient.prototype.getRoom = function(){ 1066 | return this.roomName; 1067 | } 1068 | 1069 | // getFriends returns the client User's friends as an array of strings. An empty array is returned 1070 | // if the client User has no friends. 1071 | GopherServerClient.prototype.getFriends = function(){ 1072 | return this.friends; 1073 | } 1074 | 1075 | // getStatus returns the client User's status. 1076 | GopherServerClient.prototype.getStatus = function(){ 1077 | return this.status; 1078 | } 1079 | 1080 | // statusName converts a status number into the name of the status as a string ("Available", "In Game", 1081 | // "Idle", and "Offline"). 1082 | GopherServerClient.prototype.statusName = function(status){ 1083 | if(status == undefined || status == null || status.constructor != Number || status < 0 || status > this.userStatusDefs.length-1){ 1084 | return undefined; 1085 | } 1086 | return this.userStatusDefs[status]; 1087 | } 1088 | 1089 | // voiceSupport returns true if the client's browser supports the voice chat features. 1090 | GopherServerClient.prototype.voiceSupport = function(){ 1091 | return this.browserVoiceSupport; 1092 | } 1093 | -------------------------------------------------------------------------------- /src/gopher-voice.js: -------------------------------------------------------------------------------- 1 | function GopherVoiceChat(){ 2 | //Defaults 3 | this.volume = 1; // Voice chat output volume: 0-1 4 | this.bSize = 300; // buffer size in milliseconds 5 | this.vcPing = 300; // buffer size in milliseconds 6 | 7 | //PING TIMER 8 | this.pT = 0; 9 | 10 | //INIT OBJECTS FOR MediaStream 11 | this.acO = null; // AudioContext out 12 | this.oG = null; // output gain 13 | this.mRa = [null, null]; // MediaRecorder array 14 | this.mRs = 0; // MediaRecorder switcher 15 | this.mS = null; // MediaStream/LocalMediaStream 16 | this.mO = false; // Mis is open 17 | this.vidEl = null; // Hidded