Skip to content
ppz0 edited this page Jul 17, 2017 · 3 revisions

Samples

0. Connection and reconnection

HTTP Server

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  console.log('Hello, Socket.io~');
});

Unity3D Client

using UnityEngine;
using socket.io;

namespace Sample {

    /// <summary>
    /// The basic sample to show how to connect to a server
    /// </summary>
    public class Connect : MonoBehaviour {

        void Start() {
            var serverUrl = "http://localhost:80";
            var socket = Socket.Connect(serverUrl);

            socket.On(SystemEvents.connect, () => {
                Debug.Log("Hello, Socket.io~");
            });

            socket.On(SystemEvents.reconnect, (int reconnectAttempt) => {
                Debug.Log("Hello, Again! " + reconnectAttempt);
            });

            socket.On(SystemEvents.disconnect, () => {
                Debug.Log("Bye~");
            });
        }

    }

}

1. Send and receive messages

HTTP Server

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Unity3D Client

using UnityEngine;
using socket.io;

namespace Sample {
    
    /// <summary>
    /// The basic sample to show how to send and receive messages.
    /// </summary>
    public class Events : MonoBehaviour {

        void Start() {
            var serverUrl = "http://localhost:80";
            var socket = Socket.Connect(serverUrl);

            // receive "news" event
            socket.On("news", (string data) => {
                Debug.Log(data);

                // Emit raw string data
                socket.Emit("my other event", "{ my: data }");

                // Emit json-formatted string data
                socket.EmitJson("my other event", @"{ ""my"": ""data"" }");
            });
        }

    }

}

2. Acks message

HTTP Server

var io = require('socket.io')(80);

io.on('connection', function (socket) {
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

Unity3D Client

using UnityEngine;
using socket.io;

namespace Sample {
    
    /// <summary>
    /// The sample show how to acks the message you sent.
    /// </summary>
    public class Acks : MonoBehaviour {

        void Start() {
            var serverUrl = "http://localhost:80";
            var socket = Socket.Connect(serverUrl);

            socket.On(SystemEvents.connect, () => {
                // send "ferret" event
                socket.Emit(
                    "ferret", "toby", ack => { Debug.Log(ack); } // set callback handler for Ack
                    );
            });
        }

    }

}

3. Restricting yourself to a namespace

HTTP Server

var io = require('socket.io')(80);

var chat = io
  .of('/chat')
  .on('connection', function (socket) {
    socket.emit('a message', {
        that: 'only'
      , '/chat': 'will get'
    });
    chat.emit('a message', {
        everyone: 'in'
      , '/chat': 'will get'
    });
  });

var news = io
  .of('/news')
  .on('connection', function (socket) {
    socket.emit('item', { news: 'item' });
  });

Unity3D Client

using UnityEngine;
using socket.io;

namespace Sample {

    /// <summary>
    /// The sample show how to restrict yourself a namespace
    /// </summary>
    public class Namespace : MonoBehaviour {

        void Start() {
            var serverUrl = "http://localhost:80";

            // news namespace
            var news = Socket.Connect(serverUrl + "/news");
            news.On(SystemEvents.connect, () => {
                news.Emit("woot");
            });
            news.On("a message", (string data) => {
                Debug.Log("news => " + data);
            });
            news.On("item", (string data) => {
                Debug.Log(data);
            });

            // chat namespace
            var chat = Socket.Connect(serverUrl + "/chat");
            chat.On(SystemEvents.connect, () => {
                chat.Emit("hi~");
            });
            chat.On("a message", (string data) => {
                Debug.Log("chat => " + data);
            });
        }

    }

}