Giter Site home page Giter Site logo

katboi01 / unity-socketio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kyledulce/unity-socketio

1.0 0.0 1.0 290 KB

This project is for Socket.IO Support in the Unity Game Engine in a WebGl Project.

License: MIT License

JavaScript 25.38% C# 74.62%

unity-socketio's Introduction

Unity-Socket.io Wiki

Note: This project is not actively maintained

This project is for Socket.IO Support in the Unity Game Engine in a WebGl Project. This project also includes tools to allow testing within the editor. Other Socket.Io libraries do not support WebGl like this library.

This library also allows you to build a multi platform application between Webgl and other platforms using the same code.

Table of Contents

Getting Started

Supported Versions

Unity Engine

This project was developed and tested in the 2019.4.x LTS editor using the Mono back-end and .NET 4.x. Note that 2020.x changes the WebGL Template. See here for making it work in 2020.x.

Socket.Io

This project was developed and tested for Socket.Io client version 4.x but should also work with servers that uses the same protocol (Protocol Version 5).

Installation

Download the Unity-SocketIO.unitypackage from the releases page. Drag the package into your project in the Unity Editor. The project should auto import by the Unity Editor.

Usage

The Documentation can be found in the wiki.

When you work on the project to create a WebGl App, switch the platform to standalone to be able to test the project in the editor. Only switch to WebGl when you start building the project.

Important Note: Include <script src="https://cdn.socket.io/4.0.0/socket.io.js"></script> to the head of your HTML document after you build the WebGl app or else it will not work!. No additional actions are needed for a standalone app. You can also make these changes inside a WebGl Template. See here for information.

Demo

Demo.cs

using UnityEngine;
using KyleDulce.SocketIo;

public class Demo : MonoBehaviour
{
    Socket s;

    void Start()
    {
		//The url must include "ws://" as the protocol
        s = SocketIo.establishSocketConnection("ws://localhost:3000");
        s.connect();
        s.on("testEvent", call);
    }

    void call(string d) {
        Debug.Log("RECEIVED EVENT: " + d);
        s.emit("testEvent", "test");
    }
}

The NodeJs Server

const express = require("express");
const app = express();
const port = 3000;

const server = require('http').Server(app);

server.listen(port, () => {
  console.log(`Server listening at port ${port}`);
});

const io = require("socket.io")(server, {
	cors: {
        origin: '*'
    }
});

io.on('connection', (socket) => {
	console.log("Got connection!");
	
	socket.on('testEvent', (data) => {
		console.log("Received test Event " + data);
	});
	
	soc = socket;
	socket.emit("testEvent", "Sending");
});

Result:

//Unity
Received Event: Sending
//Nodejs Server
Received test Event test

License

Distributed under the MIT License. See LICENSE For more Information.

Acknowledgements

unity-socketio's People

Contributors

kyledulce avatar

Stargazers

 avatar

Forkers

turkeys-cyber

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.