Last active
October 12, 2015 12:09
-
-
Save masayuki5160/2700bf0891381755d44e to your computer and use it in GitHub Desktop.
サーバサイドPHP, クライアントUnityで検証
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using WebSocketSharp; | |
using System.Collections.Generic; | |
public class Chat : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
Connect(); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
/// | |
/// The message which store current input text. | |
/// | |
string message = ""; | |
/// | |
/// The list of chat message. | |
/// ここはなぜか宣言が間違っていたので修正してます. | |
List<string> messages = new List<string>(); | |
/// websocketのサーバ | |
string wsserver = "ws://ip address:9000"; | |
/// Raises the GU event. | |
/// | |
/// | |
void OnGUI(){ | |
// Input text | |
message = GUI.TextArea(new Rect(0,10,Screen.width * 0.7f,Screen.height / 10),message); | |
// Send message button | |
if(GUI.Button(new Rect(Screen.width * 0.75f,10,Screen.width * 0.2f,Screen.height / 10),"Send")){ | |
SendChatMessage(); | |
} | |
// Show chat messages | |
var l = string.Join("\n",messages.ToArray()); | |
var height = Screen.height * 0.1f * messages.Count; | |
GUI.Label( | |
new Rect(0,Screen.height * 0.1f + 10,Screen.width * 0.8f,height), | |
l); | |
} | |
WebSocket ws; | |
void Connect(){ | |
ws = new WebSocket(wsserver); | |
// called when websocket messages come. | |
ws.OnMessage += (sender, e) => | |
{ | |
string s = e.Data; | |
Debug.Log(string.Format( "Receive {0}",s)); | |
messages.Add("> " + e.Data); | |
if(messages.Count > 10){ | |
messages.RemoveAt(0); | |
} | |
}; | |
ws.Connect(); | |
Debug.Log("Connect to " + ws.Url); | |
} | |
void SendChatMessage(){ | |
Debug.Log("Send message " + message); | |
ws.Send(message); | |
this.message = ""; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* EC2上でテスト | |
-- Rachetの推奨環境としてPHP5.4が必須 | |
sudo yum install httpd24 php54 | |
compserのインストール | |
curl -sS https://getcomposer.org/installer | php | |
* composerは任意のディレクトリにセットアップする | |
参考:Composer本体をインストールする | |
http://kore1server.com/175 | |
-- composerのインストール | |
/var/www/html/composer.phar install | |
*************** | |
Package guzzle/common is abandoned, you should avoid using it. Use guzzle/guzzle instead. | |
Package guzzle/stream is abandoned, you should avoid using it. Use guzzle/guzzle instead. | |
Package guzzle/parser is abandoned, you should avoid using it. Use guzzle/guzzle instead. | |
Package guzzle/http is abandoned, you should avoid using it. Use guzzle/guzzle instead. | |
*************** | |
-- composerの実行時に上記エラーが発生. | |
-- そのためguzzleのバージョンを変更 | |
/var/www/html/composer.phar require guzzle/guzzle:~3.7 | |
-- なおGuzzleについては下記を参照 | |
PHPでRESTのAPIを使うときはGuzzleというライブラリが便利 | |
http://fuzzydevelopment.net/guzzle-288.html | |
-- 下記ソースから接続先のサーバだけを変更する(IPの変更ね.) | |
-- あとはポート9000番を使用するのでそこに注意 | |
-- また、事前に起動させておくこと.これよくわかってないかも.... | |
-- 参考サイト | |
Node.js覚えたくない人のためのphpで始めるWebSocket | |
http://webuilder240.github.io/2014/10/php-websocket/ | |
Pairy : Ratchet PHP & ZeroMQ でリアルタイム通信 | |
http://timers-tech.hatenablog.com/entry/2013/11/03/181248 | |
-- ここまではok. スケールアウトさせるには? | |
-- webSocketのログインサーバつくるしかなさそう | |
-- クライアント側について | |
-- 下記プログラムの丸コピーして一部Listの宣言を修正したらうごいたよ | |
-- 接続先は上記で作成したwebsocketサーバでok | |
UnityでWebSocketを使ってチャット -Playと一緒- | |
http://blob.geishatokyo.com/archives/2013/06/21/takezoux2.html | |
調べること | |
・なぜphp chat.phpと起動しておかないとうまくいかないのか? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment