WebsocketSharpプラグインをつかって通信を成功させたいのですが、
ほぼサンプルのコードのままなのに、sendでメッセージを送ると
InvalidOperationException: The current state of the connection is not Open.
と出てしまいます。
これがコードになります。
Client側
C#
1<code> 2using System; 3using System.Collections.Generic; 4using UnityEngine; 5using WebSocketSharp; 6public class WebsocketAccessor : MonoBehaviour 7{ 8 private WebSocket ws; 9 10 void Start() 11 { 12 13 ws = new WebSocket("ws://localhost:8088/"); 14 15 ws.OnOpen += (sender, e) => 16 { 17 Debug.Log("Opended"); 18 }; 19 ws.OnMessage += (sender, e) => 20 { 21 Debug.Log("Received " + e.Data); 22 }; 23 24 ws.OnError += (sender, e) => 25 { 26 Debug.Log("ERROR"); 27 }; 28 ws.Connect (); 29 30 } 31 32 33 public void StartSend(string message) 34 { 35 // 送るボタンを押すと入力された文字列データを送信する 36 ws.Send (message); 37 38 } 39 private void OnDestroy() 40 { 41 // 接続を切る. 42 ws.Close(); 43 } 44} 45
サーバー側
C#
1<code> 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using System; 6using WebSocketSharp; 7using WebSocketSharp.Server; 8 9//exmample 10public class Program : MonoBehaviour { 11 12 void Awake () { 13 14 //var wssv = new WebSocketServer ("ws://dragonsnest.far"); 15 var wssv = new WebSocketServer (8088); 16 wssv.AddWebSocketService<Laputa> ("/Laputa"); 17 wssv.Start (); 18 //Console.ReadKey (true); 19 //wssv.Stop (); 20 21 } 22 23} 24 25―――――――--------------------------------- 26using System; 27using WebSocketSharp; 28using WebSocketSharp.Server; 29using UnityEngine; 30 31//example 32public class Laputa : WebSocketBehavior{ 33 34 protected override void OnOpen() 35 { 36 Debug.Log ("opend"); 37 } 38 39 protected override void OnMessage (MessageEventArgs e) 40 { 41 var msg = e.Data == "BALUS" 42 ? "I've been balused already..." 43 : "I'm not available now."; 44 45 Send (msg); 46 } 47}
Open というメッセージがでないので Openになっていないのですが、
どうしてOpenにならないのでしょうか?
PCで何か設定しないとブロックされてしまっているのでしょうか?
ws = new WebSocket("ws://localhost:8088/"); URLとポートがおかしいんでしょうか?
ご教授お願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/21 02:37 編集
2018/04/21 02:09
2018/04/21 02:10
2018/04/21 02:15
2018/04/21 02:21
2018/04/21 02:25
2018/04/24 02:16
2018/04/24 02:22 編集
2018/04/24 04:33
2018/04/24 05:22
2018/04/24 05:27
2018/04/24 05:35
2018/04/24 05:44