Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Created August 31, 2011 16:13
Show Gist options
  • Save mumoshu/1183918 to your computer and use it in GitHub Desktop.
Save mumoshu/1183918 to your computer and use it in GitHub Desktop.
Unity+WebSocket.cs
using UnityEngine;
using System.Collections;
using SuperWebSocket.Client;
using System;
public class Net : MonoBehaviour {
private string lastMessage = string.Empty;
public static string serverURI = "ws://192.168.100.196:12345/channels/0?userId=1";
public static WebSocket webSocket = new WebSocket(serverURI, "basic");
void Awake () {
Debug.Log("Awoke!");
webSocket.OnClose += new EventHandler(webSocketClient_OnClose);
webSocket.OnOpen += new EventHandler(webSocketClient_OnOpen);
webSocket.OnMessage += new EventHandler<MessageEventArgs>(webSocketClient_OnMessage);
webSocket.Connect();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnDestroy () {
Debug.Log("Destroy!");
webSocket.Close();
}
void webSocketClient_OnOpen(object sender, EventArgs e)
{
Debug.Log("OnOpen!");
webSocket.Send("say:Hello WebSocket from Unity!");
}
void webSocketClient_OnMessage(object sender, MessageEventArgs e)
{
lastMessage = e.Message;
Debug.Log("lastMessage = " + lastMessage);
}
void webSocketClient_OnClose(object sender, EventArgs e)
{
Debug.Log("OnClose!");
}
}
@kerryjiang
Copy link

The code is a bit old. The websocket client had been separated from SuperWebSocket.Client to the new client project WebSocket4Net long days ago.

@Lesliehdezg
Copy link

Hi, how can i download a file with websockets from a URL and save the file in specific location for example : DOWNLOADS. in Unity. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment