Skip to content

Commit

Permalink
update demo code and tested pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Liming Xie committed Jun 2, 2017
1 parent ab833e0 commit 27cc284
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 108 deletions.
Binary file added Demo/Hello.unity
Binary file not shown.
Binary file removed Demo/HelloIO.unity
Binary file not shown.
41 changes: 31 additions & 10 deletions Demo/TestSocketIOScript.cs → Demo/SocketIOScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ public class ChatData {
public string msg;
};

public class TestSocketIOScript : MonoBehaviour {
public InputField inputMsg = null;
public Button btnSend = null;
public Text textChatLog = null;
public class SocketIOScript : MonoBehaviour {
public string serverURL = "http://localhost:3000";

public InputField uiInput = null;
public Button uiSend = null;
public Text uiChatLog = null;

protected Socket socket = null;
protected List<string> chatLog = new List<string> ();

void Destroy() {
DoClose ();
Expand All @@ -25,19 +28,35 @@ void Destroy() {
void Start () {
DoOpen ();

btnSend.onClick.AddListener(() => SendChat(inputMsg.text));
uiSend.onClick.AddListener(() => {
SendChat(uiInput.text);
uiInput.text = "";
uiInput.ActivateInputField();
});
}

// Update is called once per frame
void Update () {

lock (chatLog) {
if (chatLog.Count > 0) {
string str = uiChatLog.text;
foreach (var s in chatLog) {
str = str + "\n" + s;
}
uiChatLog.text = str;
chatLog.Clear ();
}
}
}

void DoOpen() {
if (socket == null) {
socket = IO.Socket ("http://localhost:3000");
socket = IO.Socket (serverURL);
socket.On (Socket.EVENT_CONNECT, () => {
Debug.Log ("Socket.IO connected");
//Debug.Log ("Socket.IO connected");
lock(chatLog) {
chatLog.Add("Socket.IO connected.");
}
});
socket.On ("chat", (data) => {
//Debug.Log(data);
Expand All @@ -47,9 +66,11 @@ void DoOpen() {

ChatData chat = JsonConvert.DeserializeObject<ChatData> (str);
string strChatLog = "user#" + chat.id + ": " + chat.msg;
Debug.Log (strChatLog);
//Debug.Log (strChatLog);

// TODO: show it in UI with main thread, UI not allow access from back thread
lock(chatLog) {
chatLog.Add(strChatLog);
}
});
}
}
Expand Down
97 changes: 0 additions & 97 deletions Demo/TestWebSocketScript.cs

This file was deleted.

Binary file removed Demo/test-server.zip
Binary file not shown.
17 changes: 17 additions & 0 deletions Demo/test-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## test-server for socket.io-unity

This is a test-server written in javascript and run with node.js.

## Usage

```bash
# install the dependencies
npm install

# run the server
node index.js
```

Now use browser to open URL `http://localhost:3000/`, then input something to interact with server.

To test socket.io-unity, use socket.io code to access URL `http://localhost:3000`
39 changes: 39 additions & 0 deletions Demo/test-server/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>

<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat', function(data){
$('#messages').append($('<li>').text("user#" + data.id + ": " + data.msg));
});
});
</script>

<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>
25 changes: 25 additions & 0 deletions Demo/test-server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

var userId = 0;
io.on('connection', function(socket){
socket.userId = userId ++;
console.log('a user connected, user id: ' + socket.userId);

socket.on('chat', function(msg){
console.log('message from user#' + socket.userId + ": " + msg);
io.emit('chat', {
id: socket.userId,
msg: msg
});
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});
9 changes: 9 additions & 0 deletions Demo/test-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "socket.io-unity-test-server",
"version": "0.0.1",
"description": "test server in node.js for socket.io-unity",
"dependencies": {
"express": "^4.15.3",
"socket.io": "^2.0.1"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This library supports all of the features the JS client does, including events,
Mono, .NET 2.0

## Demo
* [Example code for Socket.IO](Demo/TestSocketIOScript.cs)
* [Example code for Socket.IO](Demo/SocketIOScript.cs)

Unzip the test-server.zip, it's a nodejs app for testing purpose.

Expand Down

0 comments on commit 27cc284

Please sign in to comment.