nhn / socket.io-client-unity3d Goto Github PK
View Code? Open in Web Editor NEWsocket.io-Client for Unity3D, which is compatible with socket.io v1.x
License: Other
socket.io-Client for Unity3D, which is compatible with socket.io v1.x
License: Other
Make 'socket.io-client-unity3d' work with socket.io v2.0.2 server.
When received message: "40/chat,", it throws the error:
IndexOutOfRangeException: Array index is out of range.
socket.io.Decoder.Decode (System.String data) (at Assets/Socket/socket.io/Decoder.cs:45)
I have an error like this
ArgumentException: JSON parse error: The document root must not follow by other values.
UnityEngine.JsonUtility.FromJson[PollingUrlAnswer] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:25)
socket.io.SocketInitializer+c__Iterator0.MoveNext () (at Assets/Assets/Plugins/socket.io/SocketInitializer.cs:169)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
hi,
I used this plugin ( dev branch and socket io version is 2) for connecting my game to socket io and handling Listeners and Emit.
so when I disconnect to the server my connection has closed but when i want to reconnect to the server in runtime after disconnecting , i think it cant connect to server correctly.
this is my connection script for this :
socketConnection = Socket.Connect(socketUrl);
socketConnection.On(SystemEvents.connectError , (Exception e) => {
Debug.Log("Socket.io Connection Error: " + e.ToString());
Socket.Reconnect(socketConnection);
});
socketConnection.On(SystemEvents.connect, () =>
{
Debug.Log("Connected To Server");
});
socketConnection.On(SystemEvents.reconnect, (int reconnectAttempt) =>
{
Debug.Log("Reconnecting : " + reconnectAttempt);
});
socketConnection.On(SystemEvents.disconnect, () =>
{
Debug.Log("Connection is Closed");
});
f.e when i connect to server for the first time i have this logs
[Singleton] An instance of socket.io.SocketManager is needed in the scene, so '(singleton) socket.io.SocketManager
socket.io => ws://localhost:6500/socket.io/?EIO=3&transport=websocket&sid=399_XTZwOOSCQmckAACk probed~
socket.io => ws://localhost:6500/socket.io/?EIO=3&transport=websocket&sid=399_XTZwOOSCQmckAACk ping~
socket.io => ws://localhost:6500/socket.io/?EIO=3&transport=websocket&sid=399_XTZwOOSCQmckAACk pong~
connected to server
but when i want to connect for the seccond time in runtime i just have this
socket.io => ws://localhost:6500/socket.io/?EIO=3&transport=websocket&sid=399_XTZwOOSCQmckAACk probed~
and i should mention this that i test Reconnect after an initial connection failure #34 issue and it couldnt help me
please check it and help me.
When I emit event that is consist of just event name in my node server
socket.emit('version')
Than issue occurs in client because of the parsing problem.
When I check the event using Debug.log()
It looks like ["version"]
.
So I change Socket.cs code.
before
var seperateIndex = pkt.body.IndexOf(", ");
var seperatorLen = 2;
if (seperateIndex == -1) {
seperateIndex = pkt.body.IndexOf(',');
seperatorLen = 1;
}
var eventName = pkt.body.Substring(2, seperateIndex - 3);
if (!_handlers.ContainsKey(eventName)) {
Debug.LogWarningFormat("{0} event doesn't have a handler", eventName);
break;
}
after
int seperateIndex = pkt.body.IndexOf(", ");
int seperatorLen = 2;
if (seperateIndex == -1) {
seperateIndex = pkt.body.IndexOf(',');
if (seperateIndex == -1) {
seperateIndex = pkt.body.Length - 1;
}
seperatorLen = 1;
}
string eventName = pkt.body.Substring(2, seperateIndex - 3);
if (!_handlers.ContainsKey(eventName)) {
Debug.LogWarningFormat("{0} event doesn't have a handler", eventName);
break;
}
if (eventName.Length == pkt.body.Length - 4) {
_handlers[eventName]("");
break;
}
Does it support binary now?
Send binary data, and receive binary data.
er...
env:
unity3d: 5.6.2 f1 personal
socket.io-client-unity3d: Latest version
Repeat the test two times, make sure:
when the message include '-', server can't receive this message
ps: I did not test other symbols
When I intialize a Socket I need to put JSON data in the PollingUrlAnswer class, but it does not parse correctly and I get an error. I eventually parsed the last part of the string.LastIndexOf ('}') method, but I think the code should be fixed.
Other than that, the Disconnect event does not work. Is this a bug?
Socket intitialize를 할 때 PollingUrlAnswer 클래스에 JSON 데이터를 넣어야 하는데(SocketInitializer.cs 파일의 140번째 정도의 줄) 파싱이 제대로 되지 않아 에러가 납니다. 해당 이슈에 대하여 이미 보고된 것을 봤지만 고쳐지지 않은 것 같습니다.
string.LastIndexOf('}') 메소드로 뒷부분을 잘라내서 결국 파싱을 했습니다만 코드가 고쳐져야 하지 않을까 하는 생각이 듭니다.
다른 내용이지만, Disconnect 이벤트가 동작하지 않는데 혹시 버그인가요?
Add 'disconnect' event and Add Disconnect() method in Socket object.
I have a scenario where the unity socket.io client might get started before the server is ready for connections ... I would like to have the client code repeatedly attempt connection until a connection is made. I'm having problems figuring out how ... this doesn't work
socket.On(SystemEvents.connectError , (Exception e) => {
Debug.Log("Socket.io Connection Error: " + e.ToString());
socket = Socket.Connect(serverUrl);
}
I see changes in the develop branch for disconnect and reconnect ... but when I try and use Socket.Reconnect(socket) I get errors ...
Please check and apply the modification details of this code.
room is server side function, so sorry
all samples not working because json parse exception.
here is call stack
ArgumentException: JSON parse error: The document root must not follow by other values.
UnityEngine.JsonUtility.FromJson[PollingUrlAnswer] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:25)
socket.io.SocketInitializer+c__Iterator0.MoveNext () (at Assets/Plugins/socket.io/SocketInitializer.cs:169)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Hello, Download link in the wiki in this address return 404 page of GitHub.
Download link : https://github.com/nhnent/socket.io-client-unity3d/releases/download/v1.1.1/Socket.IO-Client.unitypackage
This problem isn't always come but sometimes when it happened, I can't even send any data string. I can't force disconnect and reconnect to refresh the socket. The problem came from WebSocket.GetLastError(). Is anyone know how fix this?
protected override void RaiseOnCompletedOnDestroy() {
if (_cancelPingPong != null) {
_cancelPingPong.Dispose();
_cancelPingPong = null;
}
if (_onRecv != null) {
_onRecv.OnCompleted();
_onRecv = null;
}
if (!IsConnected) // Note : Here should be if(IsConnected)
WebSocket.Close();
}
You can do assign types to .emit(..) and .event(..) by template constructor like this:
const socketServer = new Server<ServerToClientEvents, ClientToServerEvents, InterServerEvents, SocketData>("chat");
but it's missing ability to do like this:
socketServer.of<ServerToClientEvents, ClientToServerEvents, InterServerEvents, SocketData>("/myNamespace")
No idea whats going on no matter what I try using the samples I just get 400 bad request. Is this now outdated?
ENV:
Node.JS: 8.2.1
Node.JS socket.io: 2.0.3
socket.io-client-unity3d: 1.1.1 (download form github release page)
Unity3d: 2017.1.0 f3
Server side i don't change anything.
Node.JS socket.io client can connect socket.io server.
But unity3d 2017.1 + socket.io-client-unity3d 1.1.1 can't connect to server
In this case how to find the solution?
unity3d client code:
Socket io = Socket.Connect ("http://127.0.0.1:7001/game");
throw two errors:
socket.io => socket.io - http://localhost:7001/game got WWW error:
UnityEngine.Debug:LogErrorFormat(String, Object[])
socket.io.SocketManager:<Update>m__2(Exception) (at Assets/Plugins/socket.io/SocketManager.cs:129)
UniRx.Operators.DoOnError:OnError(Exception) (at Assets/Plugins/UniRx/Scripts/Operators/Do.cs:212)
UniRx.Operators.Timeout:OnError(Exception) (at Assets/Plugins/UniRx/Scripts/Operators/Timeout.cs:110)
UniRx.Operators.FromCoroutine:OnError(Exception) (at Assets/Plugins/UniRx/Scripts/UnityEngineBridge/Operators/FromCoroutine.cs:55)
socket.io.<InitCore>c__Iterator0:MoveNext() (at Assets/Plugins/socket.io/SocketInitializer.cs:131)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
WWWErrorException: Exception of type 'UniRx.WWWErrorException' was thrown.
UniRx.InternalUtil.ThrowObserver`1[socket.io.Socket].OnError (System.Exception error) (at Assets/Plugins/UniRx/Scripts/InternalUtil/ListObserver.cs:102)
UniRx.Operators.DoOnCompletedObservable`1+DoOnCompleted[socket.io.Socket].OnError (System.Exception error) (at Assets/Plugins/UniRx/Scripts/Operators/Do.cs:272)
UniRx.Operators.DoOnErrorObservable`1+DoOnError[socket.io.Socket].OnError (System.Exception error) (at Assets/Plugins/UniRx/Scripts/Operators/Do.cs:222)
UniRx.Operators.TimeoutObservable`1+Timeout[socket.io.Socket].OnError (System.Exception error) (at Assets/Plugins/UniRx/Scripts/Operators/Timeout.cs:110)
UniRx.Operators.FromCoroutineObservable`1+FromCoroutine[socket.io.Socket].OnError (System.Exception error) (at Assets/Plugins/UniRx/Scripts/UnityEngineBridge/Operators/FromCoroutine.cs:55)
socket.io.SocketInitializer+<InitCore>c__Iterator0.MoveNext () (at Assets/Plugins/socket.io/SocketInitializer.cs:131)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
node.js client code:
const io = require('socket.io-client');
const socket = io('http://127.0.0.1:7001/game');
socket.on('connect', () => {
console.log('connect!');
});
This not working with SSL? I need to have my connection betwen server and client secured
I paste code from wiki https://github.com/nhn/socket.io-client-unity3d/wiki/Sample#2-acks-message and run nodejs server and run unity client and in log this error:
ArgumentException: JSON parse error: The document root must not follow by other values.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:50)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at C:/buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs:33)
socket.io.SocketInitializer+d__21.MoveNext () (at Assets/Plugins/socket.io/SocketInitializer.cs:138)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17
My Laptop:
Intel Pentium 2020m
4 GB RAM
Intel® HD Graphics for 3rd Generation Intel® Processors
Hi I have some questions
1.How we can disconnect the client. I want disconnect client when a user has bad internet connection.
2. Some times disconnect message received with a delay in server side. I close game and it receives after some minutes and some times I get message immediately
3. and after I upgrade to your new version I can't send any message why? I can just connect
exception thrown: ReferenceError: webSocketInstances is not defined,ReferenceError: webSocketInstances is not defined
at SocketCreate (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:66:17)
at _SocketCreate (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :2834:40)
at Array._SocketCreate__wrapper (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4574286:10)
at Object.ftCall_ii [as dynCall_ii] (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6706:46)
at invoke_ii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6693:31)
at _WebSocketWrapper_SocketCreate_m903007767 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :3435391:8)
at Array.asm._WebSocketWrapper_SocketCreate_m903007767 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4249361:57)
at Object.ftCall_iiii [as dynCall_iiii] (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6910:48)
at invoke_iiii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6897:33)
at _WebSocketWrapper_ConnectInternal_m3637984849 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :3829692:7)
at Array.asm._WebSocketWrapper_ConnectInternal_m3637984849 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4522643:61)
at Object.ftCall_viii [as dynCall_viii] (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6502:48)
at invoke_viii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6489:26)
at _WebSocketWrapper_Connect_m829512760 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :3299268:5)
at Array.asm._WebSocketWrapper_Connect_m829512760 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4440089:52)
at Object.ftCall_vii [as dynCall_vii] (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :5958:47)
at invoke_vii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :5945:25)
at Array._U3CInitCoreU3Ec__Iterator0_MoveNext_m2591192826 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :259158:18)
at mftCall_iii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4191259:100)
at __ZN21InterfaceFuncInvoker0IbE6InvokeEjP11Il2CppClassP12Il2CppObject (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :3293660:9)
at Array.asm.__ZN21InterfaceFuncInvoker0IbE6InvokeEjP11Il2CppClassP12Il2CppObject (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4253837:84)
at Object.ftCall_iiii [as dynCall_iiii] (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6910:48)
at invoke_iiii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6897:33)
at Array._SetupCoroutine_InvokeMoveNext_m2975616245 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :2668840:10)
at mftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4165159:39)
at _Z53RuntimeInvoker_Void_t1841601450_Il2CppObject_IntPtr_tPK10MethodInfoPvPS2 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4125827:2)
at Array.asm._Z53RuntimeInvoker_Void_t1841601450_Il2CppObject_IntPtr_tPK10MethodInfoPvPS2 (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4523999:94)
at Object.ftCall_iiii [as dynCall_iiii] (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6910:48)
at invoke_iiii (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :6897:33)
at __ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :3939572:8)
at _il2cpp_runtime_invoke (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4161323:9)
at asm._il2cpp_runtime_invoke (eval at loadDynamicLibrary (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:781:19), :4341179:38)
at _il2cpp_runtime_invoke (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:11640:42)
at __Z23scripting_method_invoke18ScriptingMethodPtr18ScriptingObjectPtrR18ScriptingArgumentsP21ScriptingExceptionPtrb (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2517991:8)
at __ZN19ScriptingInvocation6InvokeEP21ScriptingExceptionPtrb (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:1790861:7)
at __ZN9Coroutine14InvokeMoveNextEP21ScriptingExceptionPtr (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2039567:2)
at __ZN9Coroutine3RunEPb (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2235390:7)
at __ZN9Coroutine17ContinueCoroutineEP6ObjectPv (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2611278:8)
at mftCall_iii (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2662614:98)
at __ZN18DelayedCallManager6UpdateEi (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:1040236:33)
at mftCall_vii (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2664628:37)
at __ZZ23InitPlayerLoopCallbacksvEN44UpdateScriptRunDelayedDynamicFrameRatestruct7ForwardEv (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2665861:2)
at mftCall_v (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:2699014:35)
at __Z10PlayerLoopv (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:771322:14)
at __ZL8MainLoopv (blob:http://localhost/2faeed81-e8d1-4561-88a1-e55ddd8cf2c1:1992203:3)
at Array.UnityLoader.873d35c852dfa40f9f7c2e481511bec7.asm.__ZL8MainLoopv (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:197895:29)
at Object.ftCall_v [as dynCall_v] (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:29779:28)
at browserIterationFunc (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:3089:23)
at Object.runIter (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:3192:5)
at Browser_mainLoop_runner (blob:http://localhost/71106806-6a35-4454-ad6b-5c83fb187a2d:3127:20)
I tried to run the connection Demo, then meet a problem:
ArgumentException: JSON parse error: The document root must not follow by other values.
the json data revived like this: {"sid":"eADGfUhZX4r8EixPAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}2:40
so I change the code in class SocketInitializer from:
var textIndex = www.text.IndexOf('{'); if (textIndex != -1) { var json = www.text.Substring(textIndex); var answer = JsonUtility.FromJson<PollingUrlAnswer>(json); _urlQueries.Add("sid", answer.sid); }
to
var textIndex = www.text.IndexOf('{'); if (textIndex != -1) { int lastIndex = www.text.LastIndexOf('}'); int length = lastIndex - textIndex + 1; var json = www.text.Substring(textIndex, length); var answer = JsonUtility.FromJson<PollingUrlAnswer>(json); _urlQueries.Add("sid", answer.sid); }
then the problem has been solved
When the event data contains ", " the parse fails. Because is looking for ", " to split it and get eventName and evenData.
If I set on Socket.cs in line 322
pkt.body = "["gameFinished",{"message":"Good job!, play in the new tournament"}]";
Will cause Warning message: gameFinished",{"message":"Good job event doesn't have a handler, because is taking gameFinished",{"message":"Good job as the eventName
Most robust way to separate even name and data should be implemented.
Implements Disconnect() method which disconnects the socket manually.
ArgumentException: Unsupported protocol: https
not support https??
https는 지원하지 않는 건가요?
그리고 이건 추가적인 질문인데, AWS의 로드 밸런서를 통과하게 되면 타임아웃 에러가 계속 납니다.
How can we Emit outside Start()? I try all of the ways to emit in other methods but it's not working
could you please give me an example
this is the way I use socket
static Socket socket;
void Start()
{
socket = Socket.Connect(_networkUrl);
}
public void JoinToGame()
{
socket.Emit("joinToGame", RequestForGame(DB._myId, _myRoom).ToString());
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.