PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/1.0/client/src/myleftchatclient.as

http://as3chat.googlecode.com/
ActionScript | 481 lines | 383 code | 76 blank | 22 comment | 25 complexity | 4fd61b113b9a8be268356af5e8472482 MD5 | raw file
  1. package {
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. import org.aswing.AsWingConstants;
  5. import org.aswing.AsWingManager;
  6. import org.aswing.BorderLayout;
  7. import org.aswing.Container;
  8. import org.aswing.FlowLayout;
  9. import org.aswing.GeneralListCellFactory;
  10. import org.aswing.JButton;
  11. import org.aswing.JFrame;
  12. import org.aswing.JLabel;
  13. import org.aswing.JList;
  14. import org.aswing.JOptionPane;
  15. import org.aswing.JPanel;
  16. import org.aswing.JScrollPane;
  17. import org.aswing.JSplitPane;
  18. import org.aswing.JTextArea;
  19. import org.aswing.JTextField;
  20. import org.aswing.JWindow;
  21. import org.aswing.VectorListModel;
  22. import org.aswing.event.AWEvent;
  23. import org.myleft.RoomListCell;
  24. import org.myleft.UserListCell;
  25. import org.myleft.core.SocketConnection;
  26. import org.myleft.data.Define;
  27. import org.myleft.data.User;
  28. import org.myleft.events.SocketConnectionEvent;
  29. public class myleftchatclient extends Sprite
  30. {
  31. private var socket:SocketConnection;
  32. private var param:Object;
  33. private var host:String = 'myleft.org';
  34. private var port:int = 8222;
  35. private var myinfo:User;
  36. private var userJframe:JFrame;
  37. private var userJlist:JList
  38. private var usermodel:VectorListModel;
  39. private var roomJframe:JFrame
  40. private var roomJlist:JList
  41. private var roommodel:VectorListModel;
  42. private var mainWindow:JWindow;
  43. private var loginDialog:LoginJFrame;
  44. private var progressDialog:progressJFrame;
  45. private var chatText:JTextArea;
  46. private var mychatText:JTextArea;
  47. private var messageText:JTextField;
  48. private var toText:JTextField;
  49. private var sendButton:JButton;
  50. private var time:Date;
  51. private var mysound:mySound;
  52. public function myleftchatclient()
  53. {
  54. AsWingManager.initAsStandard(this);
  55. mysound = new mySound();
  56. time = new Date;
  57. param = root.loaderInfo.parameters;
  58. myinfo = new User;
  59. mainWindow = new JWindow();
  60. //??????
  61. var pane:Container = mainWindow.getContentPane();
  62. pane.setLayout(new BorderLayout());
  63. //??????
  64. usermodel = new VectorListModel();
  65. userJlist = new JList(usermodel, new GeneralListCellFactory(UserListCell,false,false));
  66. userJframe = new JFrame(mainWindow, "??????");
  67. //userJframe.setClosable(false);
  68. userJframe.setResizable(false);
  69. userJframe.setSizeWH(200, 200);
  70. userJframe.getContentPane().append(new JScrollPane(userJlist));
  71. userJframe.setLocationXY(stage.stageWidth-userJframe.width, 0);
  72. userJframe.show();
  73. //????
  74. roommodel = new VectorListModel();
  75. roomJlist = new JList(roommodel, new GeneralListCellFactory(RoomListCell,false,false));
  76. roomJframe = new JFrame(mainWindow, "????");
  77. //roomJframe.setClosable(false);
  78. roomJframe.setResizable(false);
  79. var roomToolJPanel:JPanel = new JPanel();
  80. var getroomlistBtn:JButton = new JButton('????');
  81. getroomlistBtn.pack();
  82. getroomlistBtn.addActionListener(getRoomList);
  83. roomToolJPanel.append(getroomlistBtn);
  84. var joinroomlistBtn:JButton = new JButton('????');
  85. joinroomlistBtn.pack();
  86. joinroomlistBtn.addActionListener(joinRoom);
  87. roomToolJPanel.append(joinroomlistBtn);
  88. roomJframe.setSizeWH(200, 200);
  89. roomJframe.getContentPane().append(roomToolJPanel, BorderLayout.NORTH);
  90. roomJframe.getContentPane().append(new JScrollPane(roomJlist), BorderLayout.CENTER);
  91. roomJframe.setLocationXY(stage.stageWidth-roomJframe.width, userJframe.height);
  92. roomJframe.show();
  93. //????
  94. chatText = new JTextArea;
  95. chatText.setWordWrap(true);
  96. chatText.setEditable(false);
  97. var scrollPane:JScrollPane = new JScrollPane(chatText);
  98. //??
  99. mychatText = new JTextArea;
  100. mychatText.setWordWrap(true);
  101. mychatText.setEditable(false);
  102. var scrollPane2:JScrollPane = new JScrollPane(mychatText);
  103. var centerSplitPanel:JSplitPane = new JSplitPane(AsWingConstants.VERTICAL, false, scrollPane, scrollPane2);
  104. pane.append(centerSplitPanel, BorderLayout.CENTER);
  105. //??
  106. var mlabel:JLabel = new JLabel("????");
  107. messageText = new JTextField;
  108. messageText.setWordWrap(true);
  109. messageText.addActionListener(sendFun);
  110. sendButton = new JButton('??');
  111. sendButton.pack();
  112. sendButton.addActionListener(sendFun);
  113. var clearButton:JButton = new JButton('????');
  114. clearButton.pack();
  115. clearButton.addActionListener(clearFun);
  116. var FriendListButton:JButton = new JButton('????');
  117. FriendListButton.pack();
  118. FriendListButton.addActionListener(friendlistFun);
  119. var shutupButton:JButton = new JButton('??');
  120. shutupButton.pack();
  121. shutupButton.addActionListener(shutupFun);
  122. var koButton:JButton = new JButton('KO');
  123. koButton.pack();
  124. koButton.addActionListener(koFun);
  125. var RoomListButton:JButton = new JButton('????');
  126. RoomListButton.pack();
  127. RoomListButton.addActionListener(roomlistFun);
  128. var quitButton:JButton = new JButton('??');
  129. quitButton.pack();
  130. quitButton.addActionListener(quitFun);
  131. messageText.setPreferredWidth(350);
  132. var toolPanel:JPanel = new JPanel(new FlowLayout);
  133. toolPanel.appendAll(mlabel, messageText, sendButton, clearButton, FriendListButton, shutupButton, koButton, RoomListButton, quitButton);
  134. pane.append(toolPanel, BorderLayout.SOUTH);
  135. //???????????
  136. mainWindow.setSizeWH(stage.stageWidth, stage.stageHeight);
  137. //????
  138. mainWindow.show();
  139. loginDialog = new LoginJFrame(mainWindow, "??", true);
  140. loginDialog.pack();
  141. loginDialog.usernameText.setText(myinfo.username);
  142. loginDialog.setLocationXY((stage.stageWidth-loginDialog.width)*0.5, (stage.stageHeight-loginDialog.height)*0.5);
  143. loginDialog.loginButton.addActionListener(loginClickHandle);
  144. loginDialog.anonymousButton.addActionListener(anonymousClickHandle);
  145. loginDialog.connectButton.addActionListener(connectClickHandle);
  146. //???
  147. progressDialog = new progressJFrame(mainWindow, "???");
  148. progressDialog.pack();
  149. progressDialog.setLocationXY((stage.stageWidth-progressDialog.width)*0.5, (stage.stageHeight-progressDialog.height)*0.5);
  150. socket = new SocketConnection;
  151. socket.addEventListener(Define.CONNECT, eventHandle);
  152. socket.addEventListener(Define.CLOSE, eventHandle);
  153. socket.addEventListener(Define.EV_TYPE_AUTH, eventHandle);
  154. socket.addEventListener(Define.EV_TYPE_AUTH_OTHER_LOGIN, eventHandle);
  155. socket.addEventListener(Define.EV_TYPE_USER_LOGIN, eventHandle);
  156. socket.addEventListener(Define.EV_TYPE_USER_LOGOUT, eventHandle);
  157. socket.addEventListener(Define.EV_TYPE_USER_ADD, eventHandle);
  158. socket.addEventListener(Define.EV_TYPE_MESSAGE, eventHandle);
  159. socket.addEventListener(Define.EV_TYPE_CHANGE_ROOM, eventHandle);
  160. socket.addEventListener(Define.EV_TYPE_ROOM_ADD, eventHandle);
  161. if (int(param['autologin'])==1)
  162. {
  163. socket.connect(host, port);
  164. progressDialog.show();
  165. }
  166. else
  167. {
  168. loginDialog.show();
  169. }
  170. stage.addEventListener(Event.RESIZE, resizeHandler);
  171. }
  172. private function eventHandle(e:SocketConnectionEvent):void
  173. {
  174. trace('eventHandle1', e.type);
  175. trace('eventHandle2', e.body.toString());
  176. progressDialog.hide();
  177. var temStr:String;
  178. switch(e.type)
  179. {
  180. case Define.CONNECT:
  181. loginDialog.setConnectEnabled(false);
  182. loginDialog.setLoginEnabled(true);
  183. if (int(param['autologin'])==1)
  184. {
  185. loginFun();
  186. }
  187. break;
  188. case Define.CLOSE:
  189. usermodel.clear();
  190. loginDialog.setConnectEnabled(true);
  191. loginDialog.setLoginEnabled(false);
  192. loginDialog.show();
  193. break;
  194. case Define.EV_TYPE_USER_LOGIN:
  195. trace("?????", e.body.attributes.username, myinfo.username);
  196. if (e.body.attributes.username!=myinfo.username) {
  197. mysound.play(mySound.online);
  198. chatText.setHtmlText( '<font color="#FF9900"><b>' + e.body.attributes.username + '</b></font> ?????' + "<br/>" + chatText.getHtmlText());
  199. }
  200. case Define.EV_TYPE_USER_ADD:
  201. usermodel.append(new User(e.body.attributes.username));
  202. trace("append");
  203. break;
  204. case Define.EV_TYPE_USER_LOGOUT:
  205. chatText.setHtmlText( '<font color="#FF9900"><b>' + e.body.attributes.username + '</b></font> ?????' + "<br/>" + chatText.getHtmlText());
  206. for (var i:int=0;i < usermodel.getSize(); i++)
  207. {
  208. if (User(usermodel.get(i)).username == e.body.attributes.username)
  209. {
  210. usermodel.remove(usermodel.get(i));
  211. break;
  212. }
  213. }
  214. break;
  215. case Define.EV_TYPE_AUTH:
  216. if (e.body.attributes.result==1)
  217. {
  218. progressDialog.hide();
  219. loginDialog.hide();
  220. myinfo.username = e.body.attributes.username;
  221. //JOptionPane.showMessageDialog("Hello", "????");
  222. }
  223. else
  224. {
  225. loginDialog.show();
  226. JOptionPane.showMessageDialog("Hello", e.body.attributes.message);
  227. }
  228. break;
  229. case Define.LOGIN_FAILURE:
  230. break;
  231. case Define.EV_TYPE_AUTH_OTHER_LOGIN:
  232. usermodel.clear();
  233. loginDialog.show();
  234. JOptionPane.showMessageDialog("Hello", e.body.attributes.message);
  235. break;
  236. case Define.EV_TYPE_MESSAGE:
  237. var ismy:Boolean = myinfo.username==e.body.attributes.from? true : false;
  238. var istomy:Boolean = myinfo.username==e.body.attributes.to? true : false;
  239. var toString:String =e.body.attributes.to ? ' ? <b>'+ e.body.attributes.to + '</b>' : '';
  240. var date:Date = new Date(int(e.body.attributes.timestamp) * 1000);
  241. var dateStr:String = " (" + date.fullYear + "-" + (date.month+1) + "-" + date.date + " " + date.hours + ":" + date.minutes + ")";
  242. var msgStr:String = e.body.firstChild.firstChild.toString();
  243. if (ismy)
  244. {
  245. msgStr = '<font color="#0066FF">' + msgStr + '</font>';
  246. }
  247. if (toString)
  248. {
  249. if (istomy)
  250. {
  251. mysound.play(mySound.msg);
  252. }
  253. mychatText.setHtmlText( '<font color="#FF6600"><b>' + e.body.attributes.from + '</b></font>'+ toString +' ?:' + msgStr + dateStr + "<br/>" + mychatText.getHtmlText());
  254. }
  255. else
  256. {
  257. if (e.body.attributes.from)
  258. {
  259. chatText.setHtmlText( '<font color="#FF6600"><b>' + e.body.attributes.from + '</b></font> ?:' + msgStr + dateStr + "<br/>" + chatText.getHtmlText());
  260. }
  261. else
  262. {
  263. chatText.setHtmlText( '<b>???</b>' + msgStr + dateStr + "<br/>" + chatText.getHtmlText());
  264. }
  265. }
  266. break;
  267. case Define.EV_TYPE_CHANGE_ROOM:
  268. getRoomList(null);
  269. roomJframe.setTitle('????-??:'+e.body.attributes.name);
  270. chatText.setHtmlText('<font color="#FF9900">?????'+e.body.attributes.name+'</font>');
  271. usermodel.clear();
  272. usermodel.append(new User('???'));
  273. break;
  274. case Define.EV_TYPE_ROOM_ADD:
  275. roommodel.append(Object({roomid:e.body.attributes.roomid, name:e.body.attributes.name+'('+e.body.attributes.num+'?)'}));
  276. break;
  277. }
  278. }
  279. //??
  280. private function clearFun(e:AWEvent):void
  281. {
  282. mychatText.setHtmlText("");
  283. chatText.setHtmlText("");
  284. }
  285. //??
  286. private function connectClickHandle(e:AWEvent):void
  287. {
  288. progressDialog.setTitle('???...');
  289. progressDialog.show();
  290. socket.connect(host, port);
  291. }
  292. //??
  293. private function loginClickHandle(e:AWEvent):void
  294. {
  295. myinfo.username = loginDialog.usernameText.getText();
  296. if (myinfo.username.length>0 && loginDialog.passwordText.getText().length>0)
  297. {
  298. progressDialog.setTitle('???...');
  299. progressDialog.show();
  300. var temp:String="<event type='"+Define.EV_TYPE_AUTH+"' ";
  301. temp += "username='"+myinfo.username+"' ";
  302. temp += "password='"+loginDialog.passwordText.getText()+"' ";
  303. temp = temp+"/>";
  304. socket.send(temp);
  305. }
  306. else
  307. {
  308. JOptionPane.showMessageDialog("Hello", "??????/??");
  309. }
  310. }
  311. //????
  312. private function anonymousClickHandle(e:AWEvent):void
  313. {
  314. progressDialog.setTitle('???...');
  315. progressDialog.show();
  316. //socket.send("<event type='100' anonymous='1'/>");
  317. socket.send("<event type='"+Define.EV_TYPE_AUTH+"' anonymous='1'/>");
  318. }
  319. //??
  320. private function loginFun():void
  321. {
  322. if (param['username'] && param['timestamp']>0 && param['sign'] && param['account'])
  323. {
  324. myinfo.username = param['username'];
  325. progressDialog.setTitle('???...');
  326. progressDialog.show();
  327. var temp:String="<event type='"+Define.EV_TYPE_AUTH+"' ";
  328. temp += "username='"+param['username']+"' ";
  329. temp += "timestamp='"+param['timestamp']+"' ";
  330. temp += "sign='"+param['sign']+"'";
  331. temp += "account='"+param['account']+"' ";
  332. temp = temp+"/>";
  333. socket.send(temp);
  334. }
  335. else
  336. {
  337. JOptionPane.showMessageDialog("Hello", "??????");
  338. }
  339. }
  340. //??????
  341. private function getRoomList(e:AWEvent):void
  342. {
  343. roommodel.clear();
  344. var toStr:String = userJlist.getSelectedValue() && userJlist.getSelectedValue().username!='???' ? "to='"+User(userJlist.getSelectedValue()).username+"'" : '';
  345. socket.send("<event type='"+Define.EV_TYPE_GET_ROOM_LIST+"'/>");
  346. }
  347. //??
  348. private function shutupFun(e:AWEvent):void
  349. {
  350. if (userJlist.getSelectedValue())
  351. {
  352. socket.send("<event type='"+Define.EV_TYPE_ADMIN_COMMAND_SHUTUP+"' username='"+userJlist.getSelectedValue().username+"'/>");
  353. }
  354. }
  355. //???
  356. private function koFun(e:AWEvent):void
  357. {
  358. if (userJlist.getSelectedValue())
  359. {
  360. socket.send("<event type='"+Define.EV_TYPE_ADMIN_COMMAND_GOOUT+"' username='"+userJlist.getSelectedValue().username+"'/>");
  361. }
  362. }
  363. //????
  364. private function joinRoom(e:AWEvent):void
  365. {
  366. if (roomJlist.getSelectedValue())
  367. {
  368. var obj:Object = roomJlist.getSelectedValue();
  369. socket.send("<event type='"+Define.EV_TYPE_CHANGE_ROOM+"' roomid='"+obj.roomid+"'/>");
  370. }
  371. }
  372. private function quitFun(e:Event):void
  373. {
  374. socket.send('<quit/>');
  375. }
  376. private function sendFun(e:AWEvent):void
  377. {
  378. if (messageText.getText().length>256)
  379. {
  380. JOptionPane.showMessageDialog("Hello", "????????256???");
  381. }
  382. else if (messageText.getText().length>0)
  383. {
  384. var toStr:String = userJlist.getSelectedValue() && userJlist.getSelectedValue().username!='???' ? "to='"+User(userJlist.getSelectedValue()).username+"'" : '';
  385. socket.send("<event type='"+Define.EV_TYPE_MESSAGE+"' "+toStr+"><body><![CDATA["+messageText.getText()+"]]></body></event>");
  386. messageText.setText("");
  387. }
  388. }
  389. private function friendlistFun(e:AWEvent):void
  390. {
  391. userJframe.show();
  392. }
  393. private function roomlistFun(e:AWEvent):void
  394. {
  395. roomJframe.show();
  396. }
  397. private function resizeHandler(e:Event):void
  398. {
  399. mainWindow.setSizeWH(stage.stageWidth, stage.stageHeight);
  400. roomJframe.setLocationXY(stage.stageWidth-roomJframe.width, userJframe.height);
  401. userJframe.setLocationXY(stage.stageWidth-userJframe.width, 0);
  402. loginDialog.setLocationXY((stage.stageWidth-loginDialog.width)*0.5, (stage.stageHeight-loginDialog.height)*0.5);
  403. }
  404. }
  405. }