- Home /
How to send Slider Information over websocket?
Hello I am using the wesocketsharp from here
I have sliders in my environment. I want to send out the slider position information through the WebSocket to my receiver (another socket). How can I do this?
Ok as suggested, here are my game object or the sliders I have.
public void initializeSliders(Slider[] sliderList_)
{
var CanvasChildren = CanvasObj.GetComponentsInChildren<Slider>();
for (int i = 0; i < CanvasChildren.Length; i++)
{
if (CanvasChildren[i].name == "Slider0")
{
sliderList_[0] = CanvasChildren[i];
sliderList_[0].minValue = lowerLimit[0];
sliderList_[0].maxValue = upperLimit[0];
sliderList_[0].value = 0;
}
else if (CanvasChildren[i].name == "Slider1")
{
sliderList_[1] = CanvasChildren[i];
sliderList_[1].minValue = lowerLimit[1];
sliderList_[1].maxValue = upperLimit[1];
sliderList_[1].value = 0;
}
else if (CanvasChildren[i].name == "Slider2")
{
sliderList_[2] = CanvasChildren[i];
sliderList_[2].minValue = lowerLimit[2];
sliderList_[2].maxValue = upperLimit[2];
sliderList_[2].value = 0;
}
}
}
I would like to get the value of the sliders when the user manipulates them in the hololens and send it over the WebSocket. ( I am new to Unity and C#)
I am trying to follow the example documentation here in github and this issue. However, any pointers would be appreciated.
Are you starting from the ground? or you have already started it . If it is can you provide the code now so we can modify it and help you :)
Hi, Paul$$anonymous$$evin I have added the game object.
Yes, I am starting from the ground.
This might help you dude :) Websocket. It has a demo maybe it can help you . Tell me if it is helpful :) . If not then i will try to help you out from the scratch :)
Answer by arunavanag · Dec 28, 2017 at 09:22 AM
So this is what I did: I create a connect function as you see below.
public void Connect()
{
//Async connection.
#if UNITY_EDITOR
Thread = new Thread(Run);
Thread.Start();
#endif
#if !UNITY_EDITOR
messageWebSocket = new MessageWebSocket();
server = new Uri("ws://" + host + ":" + port.ToString());
IAsyncAction outstandingAction = messageWebSocket.ConnectAsync(server);
AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
outstandingAction.Completed = aach;
#endif
}
#if !UNITY_EDITOR
public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
{
// Status completed is successful.
if (status == AsyncStatus.Completed)
{
//Guarenteed connection
con = true;
//Creating the writer that will be repsonsible to send a message
dataWriter = new DataWriter(messageWebSocket.OutputStream);
}
else
{
con = false;
}
}
#endif
and a sendservice method:
public void SendService(string service, string args)
{
#if UNITY_EDITOR
if (Socket != null)
{
string s = CallService(service, args);
Socket.Send(s);
}
#endif
#if !UNITY_EDITOR
if (messageWebSocket != null)
{
string s = CallService(service, args);
dataWriter.WriteString(s);
dataWriter.StoreAsync();
}
#endif
}
I have button called "Connect" which i want to use for connecting to the host ip +port , which has a method as : (I got this changestate button thingy from online source)
public void ChangeButtonState(State newState)
{
State oldState = currentState;
currentState = newState;
if (newState > oldState)
{
for (int j = (int)newState; j > (int)oldState; j--)
{
buttonRenderer.material.SetFloat("_BlendTex0" + j, 1.0f);
}
}
else
{
for (int j = (int)oldState; j > (int)newState; j--)
{
buttonRenderer.material.SetFloat("_BlendTex0" + j, 0.0f);
}
}
}
So in my Update method: I sent over my message by packing it in a string:
public void Update(){
#if UNITY_EDITOR
//
#endif
if (con)
{
communicator.ChangeButtonState(ButtonFunction.State.Selected);
}
slider_info = System.String.Format(",",sliderList[0].value.ToString(), sliderList[1].value.ToString(), sliderList[2].value.ToString(),
sliderList[3].value.ToString(),
sliderList[4].value.ToString(),
sliderList[5].value.ToString());
SendService("/holo/slider_value", slider_info);
}
con is a bool variable set to false.
I am sure I am missing out something.
I certainly cant establish any connection and see slider messages on the other end of the socket that is running.
I am receiving an error message when I play my unity editor, which I have not googled yet (new to C#).
Error Message:
NullReferenceException: Object reference not set to an instance of an object Source.Update () (at Assets/Scripts/Source.cs:54)
Kindly help.
Your answer
Follow this Question
Related Questions
Can't figure class property 0 Answers
Send data from unity to python socket 1 Answer
Multiple Cars not working 1 Answer
Vector3.MoveTowards doesnt work 1 Answer
Vector3.distance is always returning 0 4 Answers